溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

c++中的換行符怎么用

發(fā)布時間:2022-04-15 11:04:33 來源:億速云 閱讀:3298 作者:iii 欄目:編程語言

這篇文章主要介紹了c++中的換行符怎么用的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇c++中的換行符怎么用文章都會有所收獲,下面我們一起來看看吧。

c++換行符有哪些

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

endl,把緩沖槽的內(nèi)容輸出到控制臺;

\r 回車,光標移到當前行的開頭,不會換到下一行,如果接著輸出的話,本行以前的內(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)容補充:

關(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++中的換行符怎么用”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“c++中的換行符怎么用”知識都有一定的了解,大家如果還想學(xué)習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

c++
AI