5 Replies - 207 Views - Last Post: 08 September 2012 - 07:38 PM Rate Topic: -----

#1 usernameisgeneric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-September 12

ifstream displaying whole file.

Posted 08 September 2012 - 08:23 AM

Hi everyone, I'm having a little trouble concerning ifstream function. First I ask the user to for a name of a project, and then I want the program to display everyone who is involved in that project, however it displays the whole file.
cout << "Please enter the name of a project: ";
cin >> name;			

ifstream file ("project.txt");
if (file.is_open())
{
	while(file.good())
	{	
		file >> name;
		getline (file, name);
		cout << name;
	}
file.close();
}


Why is it displaying the whole text file?
By the way here is a example of the text file
"project 1"
    Bob
    Mike
"project 2"
    Jay
    Homer

Thanks.

Is This A Good Question/Topic? 0
  • +

Replies To: ifstream displaying whole file.

#2 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: ifstream displaying whole file.

Posted 08 September 2012 - 08:33 AM

Because you are looping until there is no more information in the file or a read error occurs.
while(file.good())


Jim
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1935
  • View blog
  • Posts: 5,759
  • Joined: 05-May 12

Re: ifstream displaying whole file.

Posted 08 September 2012 - 08:34 AM

Because you wrote the code to display all the contents. Nowhere in your code are you filtering what you've read from the file.
Was This Post Helpful? 0
  • +
  • -

#4 usernameisgeneric  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 08-September 12

Re: ifstream displaying whole file.

Posted 08 September 2012 - 08:46 AM

View Postjimblumberg, on 08 September 2012 - 08:33 AM, said:

Because you are looping until there is no more information in the file or a read error occurs.
while(file.good())


Jim

I changed the conditions to while(file.eof() == false) and got the same thing. And I tried to equal to name, but then I got a compiler error

View PostSkydiver, on 08 September 2012 - 08:34 AM, said:

Because you wrote the code to display all the contents. Nowhere in your code are you filtering what you've read from the file.

But how would I filter it?
Was This Post Helpful? 0
  • +
  • -

#5 jimblumberg  Icon User is offline

  • member icon

Reputation: 3058
  • View blog
  • Posts: 9,305
  • Joined: 25-December 09

Re: ifstream displaying whole file.

Posted 08 September 2012 - 09:23 AM

Quote

I changed the conditions to while(file.eof() == false) and got the same thing.

That's because it is basically the same, only this time it only exits at the end of the file.

Quote

And I tried to equal to name, but then I got a compiler error

Post your code that gave you the errors and the complete error message. But you don't want to change the loop conditions. You want to loop through the entire file until you find something that matches your criteria. You will probably need to use an if statement to determine when to exit your loop.

Jim
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 980
  • View blog
  • Posts: 3,397
  • Joined: 19-February 09

Re: ifstream displaying whole file.

Posted 08 September 2012 - 07:38 PM

View Postusernameisgeneric, on 08 September 2012 - 06:46 PM, said:

But how would I filter it?


What is constant?

The project name appears to be enveloped in quotes "project 1". A project has a name at the beginning and ends with a name or the end-of-file. You could test for double quotes at the start of a line. The data also appears to be indented.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1