file.getline()/cout problem

A. Jones netzapper at magicstar.net
Fri Apr 5 15:13:43 EST 2002


In trying to understand how file access might work in my instance, I
wrote a small test program with the expectation that it would print
off 35 lines of arbitrarily length of a file.

However, it's reading off a certain amount, and then abruptly ending
in the middle of a line.  The loop continues to run, but the strings
are apparently empty (when I put in a constant to be printed after the
string, it printed 35 times).

I was under the impression that because of the limited scope of
functions, the sTeP and teststring variable would flush each time
(while the position in the file was recorded in the global testfile
variable), and so, the buffer and string would not overflow.  Was I
incorrect?

Below is the code:


#include <iostream.h>
#include <string.h>
#include <fstream.h>

ifstream testfile("QOD.qdb");

string stringtest(void) {
string teststring;
char sTeP [1024];
testfile.getline(sTeP, 1024);
teststring.append(sTeP);
return teststring;
}

int main ()
{
	char holder;
	cout << "This is a test: ";
	for(int r=0; r<=35;r++) {
	cout << stringtest();
	}
	return 0;
}


-----------== Posted via Newsfeeds.Com, Uncensored Usenet News ==----------
   http://www.newsfeeds.com       The Largest Usenet Servers in the World!
------== Over 100,000 Newsgroups - Ulimited downloads - 19 servers ==-----



More information about the Python-list mailing list