Filestream ifstream.seekg(int) problem in Borland Turbo C++

T

Tim Müller

Hello,

I am using Borland Turbo C++ Version 3.1 for Windows with the stream
libraries included with this package.

I am trying to read and find textlables in a text file, with this
code:

BOOL FindLabel (ifstream& InputFile, char* Lable)
{
char InBuffer[81];
if (!InputFile.eof()) InputFile.getline(InBuffer,81);
while ( strcmp (InBuffer, Lable) && !InputFile.eof() )
{
InputFile.getline(InBuffer, 81);
}
if (strcmp(Lable, InBuffer)==0 && !InputFile.eof() ) return TRUE;
else return FALSE;
}


// extract of WinMain-function
{
....
ifstream InputFile ("Testfile.txt");

InputFile.seekg(0); // sets streampointer to beginning of the file
FindLable(InputFile, "Test1");

InputFile.seekg(0); // sets streampointer to beginning of the file
FindLable(InputFile, "Test2");

....
}

If my test file looks like
case1
Test2

Test1

end

both lables are found.

If it looks like
case2
Test2
Test

end

lable "Test2" is not found, because InputFile.good() goes to FALSE,
and InputFile.seekg(0) doesn't work anymore.

In case2, FindLable searches until the end of the file while trying to
find "Test1", InputFile.eof() becomes TRUE, and then seekg(0) doesn't
work anymore.

How could I set the streampointer of ifstream back to position 0,
after I read until eof?

Best regards

TM
 
J

john_andronicus

How could I set the streampointer of ifstream back to position 0,
after I read until eof?

The standard method is

InputFile.clear();
InputFile.seekg(0);

but you are working with a very old compiler so no guarantees.

Irrespective I would upgrade to a newer compiler, e.g. VC++ 2005
Express edition (which you can download for free from Microsoft)

John
 
T

Tim Müller

The standard method is

InputFile.clear();
InputFile.seekg(0);

Thank you, after short quick tests it seems to work.
but you are working with a very old compiler so no guarantees.

Yes :-(
Irrespective I would upgrade to a newer compiler, e.g. VC++ 2005
Express edition (which you can download for free from Microsoft)

Well, I know at least something about this old compiler and it's
libraries, and I need to finish some code really soon.
Upgrading means a lot of learning again, which I could do if I had
more time.

Thanks a lot
TM.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top