我有endl,但它没有进入我的文件,所以当我输入超过1行时,它全部在记事本中的同一行。
我试过了:
codeFile << codeLine; codeFile << endl;
我也尝试通过向字符串添加一个“\ n”来添加一个常量字符串,但它不起作用。
//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行结尾,而不是NotePad理解的Windows行结尾。
用endl
取代'\n'
无济于事。 endl
是一个'\n'
,随后是一个流冲洗。
最好的选择是使用不同的文件阅读器,例如,理解Unix行结尾的写字板。替代方案是
\r\n
结尾的Windows行,但这意味着您的代码在基于Unix的系统上会出现类似错误的行结束问题。