英文:
File Append In Such A Way That It Ends Line After Each Save C++
问题
fstream file("afile.txt", ios::out | ios::in | ios::app);
if (!file.is_open())
{
cout << "Error Loading File!";
}
else
{
// cout << " File Created ";
}
for (i = 0; i < gameplayed; i++)
{
file << name << " Won $" << prize << " And Answered " << questions << " Questions" << "\n";
}
file.seekg(0);
string line;
while (file.good())
{
getline(file, line);
cout << line;
}
所以基本上这段代码运行得很好,不会覆盖,但它会像这样立即开始:
"Player Won $2000 Dollars And Answered 6 Questions Player2 Won $2000 Dollars And Answered 6 Questions"
我想要的是它出现像这样,有一个换行符,而不是在其后面。
英文:
fstream file("afile.txt" , ios :: out | ios:: in | ios :: app);
if(!file.is_open())
{
cout << "Error Loading File!";
}
else
{
// cout << " File Created ";
}
for(i = 0 ; i < gameplayed ; i ++)
{
file <<name << " Won $" << prize << " And Answered " << questions << " Questions" << "\n";
}
file.seekg(0);
string line;
while(file.good())
{
getline(file,line);
cout << line ;
}
So basically the codes works perfectly and doesn't overwrite, but it just starts right after like
"Player Won $2000 Dollars And Answered 6 Questions Plater2 Won $2000 Dollars And Answered 6 Questions"
I want is To Appear Like
With a line break and not after it
答案1
得分: 2
> 将 std::endl 替换为 "\r\n",以获取回车换行(CRLF),而不仅是换行(LF)。
答案2
得分: 1
你的输出确实有换行符。
然而,它没有回车符。
如果你在Windows上阅读文件,在一个区分的应用程序中,你将看不到这个换行。
你可能希望考虑写成\r\n
,这是Windows上的标准换行符序列。
英文:
Your output does have line breaks.
However, it doesn't have carriage returns.
If you're reading the file on Windows, in an app that makes a distinction, you won't "see" the break.
You may wish to consider writing \r\n
instead, which is the standard line ending sequence on Windows.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论