1. 程式人生 > 程式設計 >c++換行符知識點總結

c++換行符知識點總結

c++換行符有哪些

\n 換行,游標移到下一行的開頭;

endl,把緩衝槽的內容輸出到控制檯;

\r 回車,游標移到當前行的開頭,不會換到下一行,如果接著輸出的話,本行以前的內容會被逐一覆蓋;

#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; 
}

結果:

this is the first line
this is the third linee
this is the fifth line
First
Second
Second23
這是換
行符
Presss any key to continue

內容補充:

關於遇到的問題例項:

遇到\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;
}

到此這篇關於c++換行符知識點總結的文章就介紹到這了,更多相關c++換行符有哪些內容請搜尋我們以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援我們!