C++ I/O Files

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

C++ I/O Files

 
0
  #1
Nov 17th, 2008
  1. int main()
  2. {
  3. string name;
  4. cout << "Please enter the filename of the encoded message:";
  5. getline(cin,name);
  6.  
  7. ifstream infile;
  8. infile.open(name.c_str());
  9.  
  10. if (infile.fail()) {
  11. cout << name << " fail to open." << endl;
  12. exit(1);
  13. }
  14.  
  15. cout << name << endl;
  16. cout << " File successfully opened" << endl;
  17.  
  18. string buffer;
  19. getline(infile, buffer);
  20.  
  21. while ( !infile.eof() ) {
  22. cout << buffer << endl;
  23. }
  24.  
  25. return 0;
  26. }
Hi I'm making a program where the user types in the name of a file and displays the context of the file in the console window. I'm trying to use getline function to read from the stream. The problem is that it reads the file and displays ONLY the first line ( it displays the first line, but it' an infinite loop) of the file to the console screen. How do I get it to display all the lines of the file to the terminal? I know I could use some type of loop, but I don't know what to do for the loop. So how do I display the other lines?
Last edited by Narue; Nov 17th, 2008 at 10:01 pm. Reason: added code tags
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,822
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 747
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Senior Bitch

Re: C++ I/O Files

 
1
  #2
Nov 17th, 2008
This is how you read a file using getline:
  1. while ( getline ( infile, buffer ) ) {
  2. // Process the line
  3. }
New members chased away this month: 3
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 9
Reputation: koman is an unknown quantity at this point 
Solved Threads: 0
koman koman is offline Offline
Newbie Poster

Re: C++ I/O Files

 
0
  #3
Nov 17th, 2008
Originally Posted by Narue View Post
This is how you read a file using getline:
  1. while ( getline ( infile, buffer ) ) {
  2. // Process the line
  3. }

I don't understand that can u post it in my code?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,836
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 503
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: C++ I/O Files

 
0
  #4
Nov 18th, 2008
Originally Posted by koman View Post
I don't understand that can u post it in my code?
  1. getline(infile, buffer);
  2.  
  3. while ( !infile.eof() ) {
  4. cout << buffer << endl;
  5. }

This is an infinite loop. Put the getline statement inside the while loop. If you put it where Narue says to put it, you can get rid of the eof part.

  1. while ( getline(infile, buffer) ) {
  2. cout << buffer << endl;
  3. }
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC