Click to See Complete Forum and Search --> : Move get pointer in ifstream


Carl
March 29th, 1999, 05:50 AM
I declared a ifstream object to process IGES file in code. IGES file is a kind of text file which has 80 characters per

line. I want to move get pointer to a specific line begining, as IGES file is fixed line length format, seekg function

should be more efficient than ignore function. I know that newline character is translated to and from carriage

return¨Clinefeed pairs in text mode, but I don't know seekg function thinks of newline character as one character or

two characters. I tried following code:

MyFile.seekg(82*nLine,ios::beg); //nLine is number of lines jumped over

and:

MyFile.seekg(81*nLine,ios::beg);

respectively.

It seemed that newline character is thought of as two characters in some files and one character in some other files.

Why?

I also tried the following code:

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

for(short i=0; i<nLine; i++)

MyFile.ignore(100,'\n');

int num = MyFile.gcount(); //this line only for check

As a result the get pointer jumped to correct position in some files while stopped somewhere before correct position

is reached in some other files, but in both case num always equals 81. Why?

Your help is greatly appreciated.

Dazza
March 29th, 1999, 09:11 AM
What about trying this?


MyFile.setmode(filebuf::binary);

Franky Braem
March 30th, 1999, 12:21 AM
Open your file in binary mode.

Carl
March 31st, 1999, 07:14 AM
Thank you for your help. I have followed your advice. But the problem remains as it was. My code fraction is:

ifstream InFile;

InFile.open( "filename", ios::in | ios::nocreate | ios::binary );

if( !InFile )

{

cout << "Open file failed";

return;

}

//The following 4 lines is intended to get number of lines

// of Start Section of file, which is recorded in the last

// line of file

char str[10];

InFile.seekg( -80, ios::end );

InFile.getline( str, 10, 'G' );

short nStartLine = atoi( str );

//Then move get pointer to begining of the first line of

//Global Section of file

InFile.seekg( 82 * nStartLine, ios::beg );

The result is correct for a.igs, for example, but the pointer jumps to the SECOND character of the first line of Global

Section of b.igs, NOT the first character(assuming nStartLine equals 1 for both files). If modifying 82 to 81, the

result is correct for b.igs, wrong for a.igs. I can't discover the difference between two files. Help me again please!

Franky Braem
March 31st, 1999, 07:47 AM
Could you send me the files so i can take a look to them.


Send it to Franky.Braem@skynet.be, because i'm not at the office anymore this week.