4 Replies - 402 Views - Last Post: 04 January 2013 - 11:52 AM Rate Topic: -----

#1 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 18-November 12

Problem with reading from file to a vector of struct

Posted 04 January 2013 - 10:15 AM

Hi!

I'm trying to read in a list from a file into a vector of struct. It reads in the first row normally and then like this:

Posted Image

and it reads the last row two times. Don't really know why. Thanks for any help!

The original file looks like this:

John|Smith|johsmi01|1.67|
Hanna|Barbera|hanbar01|1.45|
Johanna|Smith|johsmi02|1.56|
Bob|Taylor|bobtay01|1.76|

void loadList(vector <Person> &persons)
{
	string filename;
	Person newPerson;
	cout << "Reading list from a text file" << endl;
	cout << "\nType in the filename you want to open: " << endl;
	cin >> filename;

	ifstream inFile(filename.c_str());
	persons.clear();

	if(inFile.is_open())
	{
		do
		{   
			getline(inFile, newPerson.firstName, DELIM);
			getline(inFile, newPerson.lastName, DELIM);
			getline(inFile, newPerson.sign, DELIM);
			inFile >> newPerson.height;
			inFile.get();
			persons.push_back(newPerson);
		}while(!inFile.eof());

		inFile.close();
		cout << "The file has opened!" << endl;
	}
	else
	{
		cout << "The file couldn't be opened!" << endl;
	}
};



Is This A Good Question/Topic? 0
  • +

Replies To: Problem with reading from file to a vector of struct

#2 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Problem with reading from file to a vector of struct

Posted 04 January 2013 - 10:25 AM

Quote

and it reads the last row two times. Don't really know why.

This is caused by using eof() to control your loop. You should use the actual read operation to control the loop. I would recommend you also consider reading an entire line into a std::string then using a stringstream to parse that string.

                std::string line;
		while(getline(inFile, line);
		{   
		        istringstream ins(line);

			getline(ins, newPerson.firstName, DELIM);
			getline(ins, newPerson.lastName, DELIM);
			getline(ins, newPerson.sign, DELIM);
			ins >> newPerson.height;
			
			persons.push_back(newPerson);
		}


Jim

This post has been edited by jimblumberg: 04 January 2013 - 10:26 AM

Was This Post Helpful? 2
  • +
  • -

#3 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 18-November 12

Re: Problem with reading from file to a vector of struct

Posted 04 January 2013 - 11:02 AM

Thanks itīs working now! I have tried the same way as you, but it didnīt work. Maybe because I didnīt use istringstream and line.
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 4292
  • View blog
  • Posts: 13,459
  • Joined: 25-December 09

Re: Problem with reading from file to a vector of struct

Posted 04 January 2013 - 11:09 AM

I think you were leaving the new line in the input buffer with your original program. Which the first getline() would accept as it's input. You don't have this problem when using the stringstream method. I didn't actually run your program so I'm guessing about the reasons. But I'm glad you got your program to work.

Jim
Was This Post Helpful? 1
  • +
  • -

#5 krillov  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 34
  • Joined: 18-November 12

Re: Problem with reading from file to a vector of struct

Posted 04 January 2013 - 11:52 AM

Yeah, maybe. But this part is working now at least. Thanks for help again!

Kristian
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1