txt文件到达eof后如何重新设置光标位置,然后在不关闭文件重新打开的情况下再次读取

问题描述 投票:0回答:0

我试图一次又一次地从文件中读取,直到满足某些条件。但我遇到了麻烦。我尝试在

seekg(ios::beg)
行尾时重置光标位置,但这不起作用

我期待对垂直写入的 txt 文件进行排序,例如:

Tchala                                                      
2002                                                           
1400                   
Jhon                      
2000                   
1200              
Peter                                                                
4000                               
3999

这是代码 名字是名字的向量

唯一不起作用的是

seekg(0,ios::beg)
而且我不知道为什么我不能用循环重新读取文件。

while(i != names.size())
{   

getline(file, line);

   if(line.find(names[i], 0) != string :: npos)
    {
        while(counter != 8)
        {
            tempfile<<line<<endl;
            counter++;
            getline(file, line);
            cout<<i;
        }
        i++;
        //reset cursor position
        
         file.seekg(0);
    
    }
}

我试图用

prisoners.Record.txt
中的数据对名称进行排序,然后将其放入
temp.txt
文件中。

c++ file-handling
© www.soinside.com 2019 - 2024. All rights reserved.