Operating System - Linux
1753837 Members
9564 Online
108806 Solutions
New Discussion

Re: The file pointer can not come back when meet EOF?

 
Zhidian Du
Occasional Contributor

The file pointer can not come back when meet EOF?

When I use lFileLength = IN.tellg(); to get the position of file stream and lster use
IN.seekg(lFilePosition); to relocate the pointor to old position, it OK unless the pointor does not reach EOF. If the pointor reach EOF, it never come back even use IN.seekg(lFilePosition); The compiler is g++ in Linux. Using other compiler, such as CC in SUNOS5.8 is OK.

Why?
1 REPLY 1
Zhidian Du
Occasional Contributor

Re: The file pointer can not come back when meet EOF?

//When pointor reach EOF, you cannot get it back!!!!?????
#define MAX_LINE 500
#include

int main (){

long int lFileLength;

long int lFilePosition;

char sRaw[MAX_LINE];

int iLine;

ifstream IN;

IN.open("testfile.txt", ios::in, 0);

if (!IN){

cout<< "Cannot open validated file. \n";

return 0;

}

IN.seekg(0, ios::end);

lFileLength = IN.tellg();

IN.seekg(0, ios::beg);

while (!IN.eof()){ //the biggest loop

IN.getline(sRaw, 255);

if(IN.eof()){

break;

}

cout<
lFilePosition = IN.tellg();

iLine=0;

while(iLine<5 ){

if(IN.eof()){

break;

}

IN.getline(sRaw, MAX_LINE);

iLine++;

cout<<" "<
}

//return to the old position

IN.seekg(lFilePosition);

}

IN.close();

return 1;
}