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

问题描述 投票:-2回答:1

我有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";
    }
c++ filestream fstream ofstream
1个回答
1
投票

Cygwin嘲笑POSIX系统并使用Unix行结尾,而不是NotePad理解的Windows行结尾。

endl取代'\n'无济于事。 endl是一个'\n',随后是一个流冲洗。

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

  • 将编译器工具链更改为不模拟POSIX操作系统的工具链,或
  • 蛮力强制以\r\n结尾的Windows行,但这意味着您的代码在基于Unix的系统上会出现类似错误的行结束问题。
© www.soinside.com 2019 - 2024. All rights reserved.