在不关闭程序的情况下写入文件后,从文件中获取保存的数据

get saved data from files after writing into them without closing the program

本文关键字:文件 获取 保存 数据 情况下 关闭程序      更新时间:2023-10-16

我有下面的表单代码:

func1()
{
    fstream stud("student", fstream::in | fstream::out | fstream::app);
    stud << "sameer";
    stud.close();
}
func2()
{
    string name;
    fstream stud("student", fstream::in | fstream::out | fstream::app);
    stud >> name;
    stud.close();
}

这里两个函数都在同一个程序中,但即使func1关闭了stud,在func2中打开文件时,更改也不会反映出来。

从阅读器中删除fstream::app。对于fstream::app,文件指针从文件的末尾开始,因此您只需读取一个空字符串。