ifstream in text mode + seekg problem

V

Vadim Volevach

Hi,

// Under C++Builder 2007 (+ Dinkumware)
#include <fstream>
using namespace std;

int main (int argc, char* argv[])
{
// Create a DOS-Text file, that has only two lines:
// - the first line is empty ("\r\n"),
// - the second line: "ABC".
// Then:

ifstream is ("c:\\abc.txt");

while (is.peek() != 'A') // It's a dead loop here... Why?
is.seekg (1, ios::cur);

return 0;
}

Thanks,
Vadim
 
J

James Kanze

Vadim said:
// Under C++Builder 2007 (+ Dinkumware)
#include <fstream>
using namespace std;
int main (int argc, char* argv[])
{
// Create a DOS-Text file, that has only two lines:
// - the first line is empty ("\r\n"),
// - the second line: "ABC".
// Then:
ifstream is ("c:\\abc.txt");
while (is.peek() != 'A') // It's a dead loop here... Why?
is.seekg (1, ios::cur);
return 0;
}

It's undefined behavior. The only seeking you can do in a text
file is to the start of the file, to the current position, or to
a streampos returned from a call to tell[gp]().
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,009
Latest member
GidgetGamb

Latest Threads

Top