无法正确输出文件

can't get file to output correctly

我正在编写这个非常简单的程序,它使用文件输出 hello world。请记住,我希望 hello 和 world 位于不同的行上。

下面是代码:

    int main()
    {


        std::ofstream someFile("file.dat");
        someFile << "" << std::endl;

        std::fstream someOtherFile("file.dat",ios::in | ios::out);

        std::string content;

        someOtherFile << "hello" << std::endl;
        someOtherFile << "world" << std::endl;
        someOtherFile.seekg(0, ios::beg);
        std::getline(someOtherFile, content);
        std::cout << content << std::endl;
        return 0;


       }

然而,每当我 运行 以下程序时,它只打印 "hello".

任何帮助将不胜感激,请给出一个使用 fstream 的示例,而不是 ofstream 或 ifstream(我正在尝试了解 fstream 的工作原理,但我发现了一些麻烦)。

我的编译器是最新的VS。

std::getline 仅从特定文件中获取一行文本。正如 http://www.cplusplus.com/reference/string/string/getline/?kw=getline 所说:

istream& getline (istream& is, string& str);

Extracts characters from is and stores them into str until the delimitation character delim is found (or the newline character, '\n', for (2)).

在第一组 getline 和 cout 之后添加另一个 getline(..) 和 cout 语句。您将获得世界作为输出。

someOtherFile << "hello" << std::endl;
        someOtherFile << "world" << std::endl;
        someOtherFile.seekg(0, ios::beg);
        std::getline(someOtherFile, content);
        std::cout << content << std::endl;
std::getline(someOtherFile, content);
        std::cout << content << std::endl;

getline 只获取文件中的一行。要获取下一行,您需要再次调用。

#include<fstream>
#include<iostream>

using namespace std;

  int main()
    {


        std::ofstream someFile("file.dat");
        someFile << "" << std::endl;

        someFile.close();

        std::fstream someOtherFile("file.dat",ios::in | ios::out);

        std::string content;

        someOtherFile << "hello ";
        someOtherFile << "world" << std::endl;
        someOtherFile.close();

        someOtherFile.seekg(0, ios::beg);
        std::getline(someFile1, content);
        std::cout << content << std::endl;

        someFile1.close();
        return 0;


       }

这将打印您想要的答案

您有 2 行代码:

someOtherFile << "hello" << std::endl;
someOtherFile << "world" << std::endl;

他们将 2 行字符串放入 file.dat:

// file.dat
hello
world

函数 "getline()" 只从文件中获取 1 行。而 "seekg" 函数将读取位置设置为文件的第一行:其中包含 "hello".

如果要读到文件末尾:则替换:

std::getline(someOtherFile, content);
std::cout << content << std::endl;

与:

while (!someOtherFile.eof())
{
    std::getline(someOtherFile, content);
    std::cout << content << std::endl;
}

或者,如果您只需要特定行,则使用计数器变量。

顺便说一下,我只是假设您打算将变量 "content" 放在 "name" 所在的位置。

getine函数一次只读取一行,所以你应该调用getline直到文件结束。下面的代码可以帮助你。

#include <iostream>
#include <fstream>`
#include <string>
using namespace std;
int main()
{


 std::ofstream someFile("file.dat");
 someFile << "" << std::endl;

 std::fstream someOtherFile("file.dat",ios::in | ios::out);

 std::string content;

 someOtherFile << "hello" << std::endl;
 someOtherFile << "world" << std::endl;
 someOtherFile.seekg(0, ios::beg);
 while(std::getline(someOtherFile, content))
 {

  std::cout << content << std::endl;
 }
 
 return 0;
}