![]() |
|
#1
|
|||
|
|||
A problem while reading data from file to vector.Hi,
I am back again... I have written a 2*3 matrix[1,2,3;4,5,6] to file. And then I tried to read the data back to initialize a 2D C++ vector. The code is as following: CPP / C++ / C Code:
However, the vector is printed out as [1,2,3;4,5,6;6,6,6]. I tried to fix it but in vain. Anybody can point out the mistake for me? Thank u in advance. nanchuangyeyu |
|||
|
#2
|
|||
|
|||
Re: A problem while reading data from file to vector.The eof flag is set only AFTER you have tried to read past the end of file, not WHEN you read the last of something in the file.
After going through the while loop twice, your program hasn't yet encountered the end of file, since it hasn't tried to read anything that would be past the end. Thus, your loop will run a third time. This time, however, in the first run through the for loop, your program tries to read past the end of file and the eof flag is set. in_file is not in good() state anymore and it cannot read anything, so the value of m is what was last read in; 6. Thus the third row. |
|
#3
|
|||
|
|||
Re: A problem while reading data from file to vector.Thank you for your help Kimmo. So what should I do in order to obtain the desired result? I just want to stop exactly after all data in the file is read.
|
|
#4
|
|||
|
|||
Re: A problem while reading data from file to vector.Quote:
As for your problem here, you could just change CPP / C++ / C Code:
CPP / C++ / C Code:
CPP / C++ / C Code:
I must admit I'm so rusty that I don't know how you would go about checking if what you just read was the last bytes in the file. Whatever the way, I feel it might be more complicated than the way I've showed here. |
Recent GIDBlog
Programming ebook direct download available by crystalattice
| Thread Tools | Search this Thread |
| Rate This Thread | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Search and replace char in same text file | lanz | C Programming Language | 6 | 17-May-2009 13:21 |
| reading from a data file | mralam | C++ Forum | 3 | 05-Aug-2007 14:12 |
| Problem reading two text files | Alastair | C Programming Language | 4 | 30-Oct-2006 19:20 |
| problem reading in strings from a file | czk101 | C++ Forum | 1 | 22-Mar-2006 09:07 |
| Yet another CD burner problem: Lite-On LSC-24082K | Erwin | Computer Hardware Forum | 1 | 22-May-2004 12:28 |
Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The