7 Replies - 342 Views - Last Post: 04 September 2011 - 11:19 PM Rate Topic: -----

#1 Darthlemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-July 11

Problem with file input

Posted 04 September 2011 - 09:22 AM

I am trying to load text from a file. The only problem is that file has random tabs and spaces in between each piece of information.
*****Example*****

. space
-> tab
Name....... Name....->..Number..... Number
Name........Name.........Number..... Number
I don't know if i should use getline() or .get(). I have tried .get(), but i don't know what to include for getline().

This post has been edited by Darthlemi: 04 September 2011 - 09:23 AM

Is This A Good Question/Topic? 0
  • +

Replies To: Problem with file input

#2 KYA  Icon User is offline

  • g++ jameson.cpp -o beverage
  • member icon

Reputation: 3093
  • View blog
  • Posts: 19,139
  • Joined: 14-September 07

Re: Problem with file input

Posted 04 September 2011 - 09:25 AM

Use getline(). Pass in your fsteam handle and the string for storage. You can then parse/use the space as a delimiter.
Was This Post Helpful? 0
  • +
  • -

#3 Darthlemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-July 11

Re: Problem with file input

Posted 04 September 2011 - 09:50 AM

View PostKYA, on 04 September 2011 - 09:25 AM, said:

Use getline(). Pass in your fsteam handle and the string for storage. You can then parse/use the space as a delimiter.


Thanks,
One quick question what do i #include for getline()? It's just iostream correct, because i keep getting an error saying getline is undefined. (i #include <iostream> using namespace std; in my header)

This post has been edited by Darthlemi: 04 September 2011 - 10:04 AM

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is online

  • member icon


Reputation: 3992
  • View blog
  • Posts: 12,321
  • Joined: 25-December 09

Re: Problem with file input

Posted 04 September 2011 - 10:15 AM

Show your code along with the complete error messages exactly as they appear in your development environment.


Jim
Was This Post Helpful? 0
  • +
  • -

#5 Ecstatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 20-March 11

Re: Problem with file input

Posted 04 September 2011 - 10:24 AM

cin should also work... It does on my PC :smartass:
Was This Post Helpful? 0
  • +
  • -

#6 Darthlemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-July 11

Re: Problem with file input

Posted 04 September 2011 - 01:38 PM

The txt file mentioned below has 9 lines...
Name.......->..Name......Number.... Number
Name...........Name......Number.... Number
ext


// Loads in all contents from a txt file
void Store::Load(const char * path)
{
	ifstream fin;
	fin.open(path);

	if(!fin.is_open())
	{
		cout << "Nothing to load!";
	}

	int NumObjs;

	fin >> NumObjs;
	fin.ignore(INT_MAX, '\n');

	// Struct that holds data members to load in
	Item fLoad;

	// Gets all information from a file containing item information
	for(int i = 0; i < NumObjs; ++i)
	{
		getline(fin, fLoad.type, ' ');
		getline(fin, fLoad.name, ' ');
		fin >> fLoad.cost;
		fin >> fLoad.weight;

		// Adds fLoad into an array
		contents.append(fLoad);
		
		// Test to see if file in is working
		cout << "append # " << i << endl;
		cout << fLoad.type << endl;
		cout << fLoad.name << endl;
		cout << fLoad.cost << endl;
		cout << fLoad.weight << endl;

	}
}



The error is that getline is undefined.


I fixed the error by doing fin.getline(fLoad.type, 16, ' ')
now i get garbage output after the first Name.

This post has been edited by Darthlemi: 04 September 2011 - 01:53 PM

Was This Post Helpful? 0
  • +
  • -

#7 Darthlemi  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 42
  • Joined: 13-July 11

Re: Problem with file input

Posted 04 September 2011 - 02:00 PM

Fixed it all. I just used fin >> name.
Thanks Ecstatic!
Was This Post Helpful? 0
  • +
  • -

#8 Ecstatic  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 42
  • Joined: 20-March 11

Re: Problem with file input

Posted 04 September 2011 - 11:19 PM

View PostDarthlemi, on 04 September 2011 - 02:00 PM, said:

Fixed it all. I just used fin >> name.
Thanks Ecstatic!


No prob :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1