写入文件时需要帮助获取新行

Need Help Getting New Line When Writing to File

本文关键字:帮助 获取 新行 文件      更新时间:2023-10-16

我有endl,但它不会进入我的文件,所以当我输入超过1行时,它都在记事本的同一行上。

我试过:

代码文件<<代码行; 代码文件<<endl;

我还尝试通过向字符串添加常量字符串来向字符串添加"",但它不起作用。

//Writing Coded Lines to File:
if(codeFile)
{
//Relaying Feedback to User:
cout << "File has been successfully opened/created" << endl;
cout << "nWriting to file..." << endl;
for(int i = 0; i < lines; i++)
{
//Getting Non-Coded Line from User:
cin.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
cin.getline(line, length);
//Creating Copy of Line:
strcpy(cline, line);
//Gathering Line Length:
length = strlen(line);
//Coding Line:
codedLine = code(length, line, cline, sAlphabet);
//Coded Line Test
cout << codedLine;
//Writing to File:
codeFile << codedLine << endl;
}
}
//Closing File:
codeFile.close();
cout << "nFile has now been closed";
}

> Cygwin模拟了POSIX系统并使用Unix行尾,而不是记事本理解的Windows行尾。

'n'替换endl无济于事。endl'n'后跟流冲洗。

最好的选择是使用不同的文件阅读器,例如写字板,它可以理解Unix行尾。替代方案是

  • 将编译器工具链更改为不模拟 POSIX 操作系统的工具链,或者
  • 暴力破解以rn结尾的Windows行,但这意味着您的代码在基于Unix的系统上将出现类似的错误行尾问题。