簡體   English   中英

C ++ std :: cout和字符串打印順序錯誤

[英]C++ std::cout and string printing in wrong order

我有一個使用g ++編譯器在Linux上運行的簡單程序:

#include <string>
#include <sstream>
#include <iostream>
#include <fstream>

using namespace std;

int main(int argc, char **argv){
 fstream file;
 string s;
 file.open("sample/dates.dat", fstream::in);
 if(!file.good())
  return 0;
 getline(file, s);
 cout << s << "." << endl;
 return 0;

}

編譯: g++ -o test test.cpp 當我運行此命令時,句號是在字符串s之前而不是之后打印的。 有人知道為什么會這樣嗎? 而且容易修復嗎?

謝謝。

如果字符串的末尾有回車符,則在打印時會將輸出位置移至控制台行的開頭。

#include <iostream>

int main()
{
    std::cout << "some line\r" << "." << std::endl;
    //                     ^^ carriage return
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM