c++換行符知識點(diǎn)總結(jié)
更新時間:2020年03月03日 10:14:51 作者:angryTom
在本篇文章里小編給大家整理的是關(guān)于c++換行符知識點(diǎn)總結(jié),需要的朋友們可以參考學(xué)習(xí)下。
c++換行符有哪些
\n 換行,光標(biāo)移到下一行的開頭;
endl,把緩沖槽的內(nèi)容輸出到控制臺;
\r 回車,光標(biāo)移到當(dāng)前行的開頭,不會換到下一行,如果接著輸出的話,本行以前的內(nèi)容會被逐一覆蓋;
#include <iostream>
using namespace std;
int main()
{
cout << "this is the first line\n";
cout << "this is the second line\r";
cout << "this is the third line\n";
cout << "this is the fouth line\r";
cout << "this is the fifth line\n";
cout<<"First"<<"\n"<<"Second"<<endl;
cout<<"First123"<<"\r"<<"Second"<<endl;
cout<<"這是換"<<endl<<"行符";
return 0;
}
結(jié)果:
this is the first line this is the third linee this is the fifth line First Second Second23 這是換 行符 Presss any key to continue
內(nèi)容補(bǔ)充:
關(guān)于遇到的問題實例:
遇到\r獲取\n的時候,替換為\0.
#include<string.h>
#include<stdio.h>
int main(int argc, char *argv[])
{
char str[128];
while (fgets(str, 127, stdin)) {
char *tmp = NULL;
//去掉換行符
if (tmp = strstr(str, "\n"))
*tmp = '\0';
//去掉回車符
if (tmp = strstr(str, "\r"))
*tmp = '\0';
printf("---%s---\n", str);
}
return 0;
}
到此這篇關(guān)于c++換行符知識點(diǎn)總結(jié)的文章就介紹到這了,更多相關(guān)c++換行符有哪些內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:
相關(guān)文章
c#之獲取本機(jī)主機(jī)名的四種方式總結(jié)
這篇文章主要介紹了c#之獲取本機(jī)主機(jī)名的四種方式總結(jié),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07
C#反射調(diào)用dll文件中的方法操作泛型與屬性字段
這篇文章介紹了C#反射調(diào)用dll文件中的方法操作泛型與屬性字段,文中通過示例代碼介紹的非常詳細(xì)。對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-05-05

