从文件内容更改后使用ifstream从同一文件(直到EOF)读取 [英] Read from same file (until EOF) using ifstream after file contents change

查看:258
本文介绍了从文件内容更改后使用ifstream从同一文件(直到EOF)读取的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要求:





我必须阅读,直到EOF(16字节a
时间)从一个特定的文件,然后
然后说睡眠5秒。现在,
5秒钟后,当我尝试从文件(其内容将
到那个时候已经附加)读
,在
预期设计必须是这样的这样
,它从那里
离开之前,再次扫描
含量(16字节的时间),直到达到EOF
点的读取。




我已经写了(基本)代码从给定文件读取(直到EOF - 16字节的时间),使用ifstream的如下:

  #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);

//获取文件的大小
Read.seekg(0,ios :: end);
length = Read.tellg();
Read.seekg(0,ios :: beg);


for(i = 0; i {

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

//读取在16byte段或EOF()中的文件,以先到者为准
//测试函数的返回条件是优选的,而不是测试EOF ()

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



//输出是:
//内容:BD8d3700indiaC#E
//内容:BD6d4700godgeD3E
// Contents:BD9d1311badge3TE


return 0;
}

我需要修改以符合我的要求。我尝试使用seekg()调用,但不知何故失败。我想知道,当我第一次访问,并从文件到文件流读取,不知何故该方案将放置一个排它锁在文件上,这将意味着我将无法从它的下一次读左右



任何人都可以告诉我该怎么做?



文件名:from4to5
内容:


BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TE


在5秒内,其他进程将写入(追加)到同一文件from4to5
现在,



文件的内容:




BD8d3700indiaC#EBD6d4700godgeD3EBD9d1311badge3TEBD6d1210clerk41EBD2d1100mayor47EBD4d2810bread6YE




现在,当程序从文件from4to5读,它必须从先前留下16字节直到遇到EOF一个时间

$ b中的点读
$ b

这次输出的意向是:

  //输出是:
//内容:BD6d1210clerk41E
//内容:BD2d1100mayor47E
//内容:BD4d2810bread6YE


解决方案

您必须:

保存您的位置

关闭文件

重新打开文件

搜索您保存的位置并继续阅读,直到EOF


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

解决方案

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

这篇关于从文件内容更改后使用ifstream从同一文件(直到EOF)读取的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆