之前在做一个project的时候,要用到文件。
尝试着在文件末尾添加数据,而不用重新建立一个新的文件追加数据后,再把旧文件删去。
网上查了下,可以用 eof 判断是否到了文件尾:
-
<span style="font-size:18px;">string n;
-
fstream file;
-
file.open(".\\treeInfo\\testID.txt");
-
if (!file)
-
{
-
cout << "open error!" << endl;
-
}
-
while (!file.eof())
-
{
-
getline(file,n);
-
cout << n<<endl;
-
}
-
n = "追加内容";
-
file.clear();
-
file << n<<endl;
-
file.close()