Can有人可以帮我解决这段代码的问题吗?我收到一堆垃圾值!写入文本文件和读取文本文件时的垃圾值
fstream fs("hello.txt");
if(fs.is_open())
{
string s = "hello";
string line;
fs << s;
while(getline(fs,line))
{
cout << line;
}
cin.get();
}
fs.close();
非常感谢,但是当我尝试这样做时,我得到的垃圾也是一样的。我试图改写与世界的第一声问候,并试图打印线
fstream fs("hello.txt");
if(fs.is_open())
{
string s = "hello";
string line;
fs << s << endl;
fs.seekg(0);
fs << "world" << endl;
fs.seekg(0);
while(getline(fs,line))
{
cout<<line;
}
cin.get();
}
fs.close();
非常感谢您,但我编辑了该帖子中显示的代码。现在我得到相同的垃圾值。 – brett 2010-07-06 07:16:46
@brett:不可重复。我在屏幕上获得了“世界”。 (只有在你写完所有内容之后调用'seekg(0)'*)。 – kennytm 2010-07-06 07:27:26