12 Replies - 546 Views - Last Post: 24 January 2013 - 03:58 PM Rate Topic: -----

#1 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Problem with reading in a csv file

Posted 22 January 2013 - 10:05 PM

It gets stuck in a loop and only seems to read the whole first line into
.city. I've never read in data with csv so any help is appreciated!

void loadData ( List<FootballTeam>& data)
{
	FootballTeam team;

	ifstream inFile ( "NFL_Coaches.csv" );

	if ( !inFile )
	{
		cout << "The file could not be found! \n";
		exit(0);
	}

		getline (inFile, team.city); 
	
		cout << team.city << endl; //used to test if code is being read in correctly

inFile.ignore();
		getline ( inFile , team.teamName);
		
inFile.ignore();
		getline (inFile, team.coach);
		

	inFile >> team.yearsofExperience;


	while ( !inFile.eof() )
	{
		data.addMember ( team );

		getline (inFile, team.city); 
		inFile.ignore();

		cout << team.city << endl;

		getline ( inFile , team.teamName);
		inFile.ignore();
		
		getline (inFile, team.coach);
		inFile.ignore();

		inFile >> team.yearsofExperience;
	}



I've tried moving around the .ignores to see if it was that, but I'm still getting the same result

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with reading in a csv file

#2 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 22 January 2013 - 10:49 PM

Please post a small sample of your input file.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Re: Problem with reading in a csv file

Posted 23 January 2013 - 10:16 PM

There is alot of data, but here's the first few.Each is in their own cell (from excel)
St. Louis	Rams	Jeff Fischer	17
San Francisco	49ers	Jim Harbaugh	1
Chicago	Bears	Lovie Smith	8
Cincinnati	Bengals	Marvin Lewis	9
Buffalo	Bills	Chan Gailey	4
Denver	Broncos	John Fox	10
Cleveland	Browns	Pat Schurmur	1
Tampa Bay	Buccaneers	Greg Schiano	0
Arizona	Cardinals	Ken Whisenhut	5
San Diego	Chargers	Norv Turner	16
Kansas City	Chiefs	Romeo Crennel	7



Was This Post Helpful? 0
  • +
  • -

#4 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 1343
  • View blog
  • Posts: 4,604
  • Joined: 19-February 09

Re: Problem with reading in a csv file

Posted 23 January 2013 - 10:40 PM

A csv file is a text file you may be able to open it with a notepad or text editor, getline gets a whole line of text you could try printing the lines.

  string line;

  while( getline (inFile, line) )
  {
     cout << line << endl; 
  }


Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 24 January 2013 - 06:47 AM

This is not actually a CSV file since there are no commas separating the fields. However it does seem to be in a tab separated format so you can use this fact to process each line. I recommend reading the entire line into a std::string using getline then using a stringstream to parse that string.

std::string line;
std::string team_city, team_name, team_coach;
int coach_experiance;
while(getline(inFile,line))
{
   std::istringstream ins(line);
   getline(ins, team_city, '\t');
   getline(ins, team_name,'\t');
   getline(ins, team_coach, '\t');
   ins >> coach_experiance;
}


Of course you will need to change the variables to match your's but this should ge you going in the correct direction.


Jim
Was This Post Helpful? 0
  • +
  • -

#6 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Re: Problem with reading in a csv file

Posted 24 January 2013 - 10:42 AM

If I were to manually put the commas in,how would I go about doing that? Would I do it in VS or excel?
Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 24 January 2013 - 10:52 AM

Quote

If I were to manually put the commas in,how would I go about doing that? Would I do it in VS or excel?

I believe Excel has the option of exporting files in the CSV format, just make sure you select the correct delimiter, the comma. However there is nothing wrong with a tab delimited file, the only difference is the delimiting character.

Jim
Was This Post Helpful? 0
  • +
  • -

#8 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Re: Problem with reading in a csv file

Posted 24 January 2013 - 12:00 PM

Okay. I did that. Now the commas appear. However, it's still not reading in right. As soon as it begins reading in, it crashes. I have no idea what to do.
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 24 January 2013 - 01:46 PM

Post your current code.

Jim
Was This Post Helpful? 0
  • +
  • -

#10 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Re: Problem with reading in a csv file

Posted 24 January 2013 - 02:55 PM

void loadData ( List<FootballTeam>& data)
{
	FootballTeam team;

	ifstream inFile ( "NFL_Coaches.csv" );

	//Verify that file is found
	if ( !inFile )
	{
		cout << "The file could not be found! \n";
		exit(0);
	}

	//Read in data
	getline (inFile, team.city); 

	inFile.ignore();
	getline ( inFile , team.teamName);
		
	inFile.ignore();
	getline (inFile, team.coach);
		
	inFile >> team.yearsofExperience;

	while ( !inFile.eof() )
	{
		data.addMember ( team );

		getline (inFile, team.city, ','); 
		inFile.ignore();

		getline ( inFile , team.teamName, ',');
		inFile.ignore();
		
		getline (inFile, team.coach, ',');
		inFile.ignore();

		inFile >> team.yearsofExperience;
	}
	
}


Was This Post Helpful? 0
  • +
  • -

#11 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 24 January 2013 - 03:11 PM

I suggest you put a cout statement after all of your extraction operations, both the getline() and extraction operator>> so you can see why your program is not working correctly.

Also you need to realize that getline() extracts and discards the delimiter, but the extraction operator>> will not. This is why I suggested using the stringstream. With the stringstream you don't need to worry about the delimiters, either the comma, or the end of line character.

Also the way you are using eof() to control your entry loop will lead to having one extra line retrieved.

Jim
Was This Post Helpful? 0
  • +
  • -

#12 Nela12  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 35
  • Joined: 16-October 12

Re: Problem with reading in a csv file

Posted 24 January 2013 - 03:28 PM

Added 12 cout << "hello" << endl; Outputted with 14 before it crashed.

[code]
Hello
Hello
Hello
Hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
hello
Press any key to continue . . .
[code]

I would use the string::stream but I'm still learning. I want to make sure I am doing this right before trying alternatives. PS: You have been really helpful. I really do appreciate it, thanks :bigsmile:
Was This Post Helpful? 0
  • +
  • -

#13 jimblumberg  Icon User is offline

  • member icon


Reputation: 4066
  • View blog
  • Posts: 12,548
  • Joined: 25-December 09

Re: Problem with reading in a csv file

Posted 24 January 2013 - 03:58 PM

I meant to print out the value you're trying to retrieve from the file, not just hello.

I also don't see any indication of a crash. It looks more like a successful program termination.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1