Sign in to follow this  

file i/o

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I have the foolowing code where i open files one at a time and read through their contents, where "filename", in the below code will change, to differents names. The problem is that the customerListFile.getline(line, 256) function, only works correctly for the first file, once the second file is load, and gets to the while ( customerListFile.getline(line, 256) ) it doesnt even eneter the loop, as if it has kept the end of file state, from the last file read, resulting in only the first file ever being read

fstream customerListFile;
	
	

	while(done == false)
	{


	customerListFile.open (fileName);

	
	while ( customerListFile.getline(line, 256) )	//read a line, with a maximum of 256 chars, and stores those in line
	{
		
		//read in file stuff				

	}

	customerListFile.close();

	if(tempCustomer.getName() != "")
		customerList.push_back(tempCustomer);


	}

Share this post


Link to post
Share on other sites
It's usually a bad idea to reuse stream objects like that. You should generally just create a new stream for reading a new file. What's probably happening is that the eof status bit is getting set when reading the first file, so the state is still bad when you open a new file. Either use a new stream or use the clear() member function to reset the status bits of the stream.

Share this post


Link to post
Share on other sites
Quote:
Original post by SiCrane
It's usually a bad idea to reuse stream objects like that. You should generally just create a new stream for reading a new file. What's probably happening is that the eof status bit is getting set when reading the first file, so the state is still bad when you open a new file. Either use a new stream or use the clear() member function to reset the status bits of the stream.


Right on the mark.

And while we're at it, prefer to use std::string as your destination for reading instead of a char[] buffer, and then use the free function std::getline so that you can read into the string.


while (!done) { // "while not done". Sure sounds a lot more idiomatic to me than
// "while done is false"...
fstream customerList(fileName); // don't add 'file' to the name;
// the fact that it's a stream is evident from the fact that you're
// reading from it, and the fact that it's a file rather than the
// console input or a std::stringstream is *not relevant*.
string line; // why do think you'd need wider scope?
while (getline(customerList, line)) {
// do whatever
}
// no need to .close()
if (tempCustomer.getName() != "") { // there's probably a better way to
// do that, too, but it depends on the rest of your code...
customerList.push_back(tempCustomer);
}
}



Yes, It's Really That Easy(TM).

Share this post


Link to post
Share on other sites

This topic is 3631 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this