GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 06-May-2009, 03:30
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past
Red face

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:
#include <cmath>
#include <string>
#include <iostream>
#include <sstream>
#include <fstream>
#include <vector>

using namespace std;

int main()
{
	ofstream out_file;
	ifstream in_file;
    
	char *out_name = "test.bin";
	out_file.open(out_name,ios::binary | ios::app);

	short matrix[2][3] = {1,2,3,4,5,6};
	// write matrix to a disk file.
	for (int y=0; y<2; y++)
	{
		for (int x=0; x<3; x++)
		{
			out_file.write(reinterpret_cast<char*>(&matrix[y][x]),sizeof(short));
		}
	}
	out_file.close();
	cout<< "Matrix has been written to test.bin."<<endl;

	vector< vector<short> >vec_2d;
	vector<short>row;

 	in_file.open(out_name,ios::binary);
	short m;
	while(!in_file.eof())
	{
		for (int y=1; y<4; y++)
		{
			in_file.read(reinterpret_cast<char*>(&m),sizeof(short));
			row.push_back(m);
			if (y == 3)
			{
				vec_2d.push_back(row);
				row.clear();
			}
		}
	}
        for (int i=0; i< vec_2d.size(); i++)
        {
	      for (int j=0; j< vec_2d[i].size(); j++)
	      {
		     cout<<" "<<vec_2d[i][j];
	      }
	      cout<<endl;
        }
        in_file.close();
        return 0;
}

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  
Old 06-May-2009, 08:08
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

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  
Old 06-May-2009, 08:50
nanchuangyeyu nanchuangyeyu is offline
New Member
 
Join Date: Apr 2009
Posts: 15
nanchuangyeyu has a little shameless behaviour in the past

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  
Old 06-May-2009, 11:20
Kimmo Kimmo is offline
Member
 
Join Date: Mar 2007
Location: Finland
Posts: 285
Kimmo is a jewel in the roughKimmo is a jewel in the roughKimmo is a jewel in the rough

Re: A problem while reading data from file to vector.


Quote:
Originally Posted by nanchuangyeyu
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.
When reading a matrix from a file, you probably know already how many rows and how many columns there are in the matrix? In other words, you know how much data you need to read. So instead of reading all the data until the end of the file is reached, you could read the data as far as you need to and only check if the end of the file is reached before all the expected data was read.

As for your problem here, you could just change
CPP / C++ / C Code:
in_file.read(reinterpret_cast<char*>(&m),sizeof(short));
row.push_back(m);
to
CPP / C++ / C Code:
in_file.read(reinterpret_cast<char*>(&m),sizeof(short));
if (in_file.eof()) // if at end of file, break loop
    break;
row.push_back(m);
Or if you're not a fan of break
CPP / C++ / C Code:
if (!in_file.eof())
    row.push_back(m);

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 GIDBlogProgramming ebook direct download available by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

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

All times are GMT -6. The time now is 15:35.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.