943,737 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 743
  • C++ RSS
Sep 1st, 2008
0

Input file data

Expand Post »
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?

C++ Syntax (Toggle Plain Text)
  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;
Similar Threads
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008
Sep 1st, 2008
0

Re: Input file data

It should be
C++ Syntax (Toggle Plain Text)
  1. readfile >> sample;
since you are getting input, not outputting.
Reputation Points: 33
Solved Threads: 18
Junior Poster in Training
mahlerfive is offline Offline
77 posts
since Aug 2008
Sep 2nd, 2008
0

Re: Input file data

Well.... If you want to use the getline function, here's a simple program to test that:

C++ Syntax (Toggle Plain Text)
  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. }
Reputation Points: 37
Solved Threads: 7
Junior Poster
raul15791 is offline Offline
102 posts
since Jun 2008
Sep 2nd, 2008
0

Re: Input file data

> 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.
Team Colleague
Reputation Points: 5862
Solved Threads: 950
Posting Sage
Salem is offline Offline
7,164 posts
since Dec 2005
Sep 2nd, 2008
0

Re: Input file data

Thanks for the help I'll check it out
Reputation Points: 104
Solved Threads: 27
Posting Whiz in Training
dmanw100 is offline Offline
239 posts
since Apr 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Need help
Next Thread in C++ Forum Timeline: editing source files over FTP





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC