Input file data

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Input file data

 
0
  #1
Sep 1st, 2008
I was wondering if anyone was familiar with reading data from a file into your program. I tried using the file stream the same way cout is used but it doesnt seem to work properly. Would the getline(); function work? If so, how would that be implemented?

  1. #include <iostream>
  2. #include <fstream>
  3. #include <strings>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. fstream readFile;
  9. readFile.open("Input.txt");
  10. string sample;
  11. readfile<<sample;
  12. cout<<"Here is a line: "<<sample;
  13. return 0;
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 77
Reputation: mahlerfive is an unknown quantity at this point 
Solved Threads: 16
mahlerfive mahlerfive is offline Offline
Junior Poster in Training

Re: Input file data

 
0
  #2
Sep 1st, 2008
It should be
  1. readfile >> sample;
since you are getting input, not outputting.
Reply With Quote Quick reply to this message  
Join Date: Jun 2008
Posts: 89
Reputation: raul15791 is an unknown quantity at this point 
Solved Threads: 7
raul15791 raul15791 is offline Offline
Junior Poster in Training

Re: Input file data

 
0
  #3
Sep 2nd, 2008
Well.... If you want to use the getline function, here's a simple program to test that:

  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8. int line_number = 1;
  9. string line;
  10. ifstream myfile("test.txt");
  11.  
  12. if(myfile.is_open())
  13. {
  14. while (!myfile.eof())
  15. {
  16. getline(myfile, line);
  17. cout << "line " << line_number << " --> " << line << endl;
  18. line_number++;
  19. }
  20. }
  21.  
  22. else
  23. {
  24. cout << "The file cannot be opened!!" << endl;
  25. }
  26.  
  27. system("PAUSE");
  28. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: Input file data

 
0
  #4
Sep 2nd, 2008
> while (!myfile.eof())
http://faq.cprogramming.com/cgi-bin/...&id=1043284351

Use
while ( getline(myfile, line) )
Try it with a small text file (say 5 lines) and note the difference.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 160
Reputation: dmanw100 is on a distinguished road 
Solved Threads: 12
dmanw100's Avatar
dmanw100 dmanw100 is offline Offline
Junior Poster

Re: Input file data

 
0
  #5
Sep 2nd, 2008
Thanks for the help I'll check it out
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC