Read from same file (until EOF) using ifstream after file contents change

只愿长相守 提交于 2019-12-19 10:47:24

问题


Requirement :


I must read until EOF (16 bytes a time) from a particular file , and then say sleep for 5 seconds. Now, after 5 seconds, when I try to read from the file (whose contents would have been appended by that time), the intended design must be in such a way that it reads from the point where it left previously and again scan the contents (16 bytes a time) until EOF is reached.

I have written (basic) code to read from the given file (until EOF - 16 bytes a time) using ifstream as follows :

#include <stdio.h> 
#include <fstream>
#include <iostream>
#include <sstream>
using namespace std;


int main() 
{ 

    int fd, i, j, length, pos;
    char buffer[100][16];
    ifstream Read;
    std::ostringstream oss;
    int current_position = 0;
    Read.open("from4to5", ios::binary);

    //Get the size of the file
    Read.seekg(0, ios::end);
    length = Read.tellg();
    Read.seekg(0, ios::beg);


    for(i=0; i<length; i++)
    {

         buffer[i][16] = '\0';
    }

    //Read the file in 16byte segments or eof(), whichever comes first
    //Testing the return condition of the function is preferred, as opposed to testing eof()

    while(Read.get(buffer[i], 17))
    {
        for(j=0; j<=16; j++)
            oss << buffer[i][j];
        cout << "Contents : " << oss.str() << endl;
        oss.seekp(0);
        i++;
    }



    // Output is :
    // Contents : BD8d3700indiaC#E
    // Contents : BD6d4700godgeD3E
    // Contents : BD9d1311badge3TE


    return 0;
}

I need to modify this to suit my requirement. I tried using the seekg() call, but somehow failed. I was wondering if, when the first time I accessed and read from the file into filestream, somehow the program would have placed an exclusive lock on the file, which would mean that I'll not be able to read from it the next time around.

Can anyone show me how it's to be done?

Filename : "from4to5" Contents:

BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TE

Within 5 seconds, some other process writes(appends) to the same file "from4to5" Now,

File Contents:

BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TEBD6d1210clerk41EBD2d1100mayor47EBD4d2810bread6YE

Now, when the program reads from the file "from4to5", it must read from the point where it left previously, 16 bytes a time until it encounters EOF.

Intention for output this time around is :

// Output is :
// Contents : BD6d1210clerk41E
// Contents : BD2d1100mayor47E
// Contents : BD4d2810bread6YE

回答1:


You'll have to:
save your position
close the file
reopen the file
seek to your saved postion and resume reading until EOF




回答2:


You should be able to clear the EOF flag on the input stream and continue reading.

while (true)
{
    while(Read.get(buffer[i], 17))
    {
        for(j=0; j<=16; j++)
                oss << buffer[i][j];
        cout << "Contents : " << oss.str() << endl;
        oss.seekp(0);
        i++;
    }
    Read.clear();
    sleep(5);
}

You could run into problems if you are reading at the same time that the file is being written, where you are not able to read all 16 bytes. This can lead to some intermittent, hard to track down bugs. At the very least, you should add in some error checking.



来源:https://stackoverflow.com/questions/1867067/read-from-same-file-until-eof-using-ifstream-after-file-contents-change

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!