943,632 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1771
  • C++ RSS
Sep 16th, 2006
1

c++ File I/O Help

Expand Post »
Hi,

I am in need of some help with a small portion of my program.

I have an input file as follows:

C++ Syntax (Toggle Plain Text)
  1. 10 9 15 program1
  2. 11 1 30 important
  3. 12 11 10 junk program
  4. 14 15 5 other program
  5. 17 6 20 another program

I need a function to open the file, read in each line and separate the three integers in the front of the line to separate int variables. I then need to separate the rest of the line into a single string variable. I have not done toom much with file IO in c++ and I am not sure how I can complete this. Here is what I have tried. Thank you for your help in advance. Here is a sample program of what I am talking about.
C++ Syntax (Toggle Plain Text)
  1. #include <fstream>
  2. #include <iostream>
  3. #include <string>
  4. using namespace std;
  5.  
  6. int main(void)
  7. {
  8. int a,b,c;
  9. string name;
  10. ifstream infile;
  11.  
  12. infile.open("file.data", ios::in);
  13.  
  14.  
  15. infile >> a >> b >> c >> name;
  16. cout << a << ' ' << b << ' ' << c << ' ' << name << endl;
  17. infile >> a >> b >> c >> name;
  18. cout << a << ' ' << b << ' ' << c << ' ' << name << endl;
  19. infile >> a >> b >> c >> name;
  20. cout << a << ' ' << b << ' ' << c << ' ' << name << endl;
  21.  
  22. infile.close();
  23.  
  24. return 0;
  25. }

Output:
C++ Syntax (Toggle Plain Text)
  1. [nnisi@vulcan heap]$ ./a.out
  2. 10 9 15 program1
  3. 11 1 30 important
  4. 12 11 10 junk

The above program works until the third one which has a string with a space in it does not work properly. Does anyonw know how I can change this? Thanks for any and all help!

Nick
Similar Threads
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005
Sep 16th, 2006
1

Re: c++ File I/O Help

If you have someIstream >> someString it will read up to the first whitespace. Try getline(infile, name) after you get the 3 integers. (I haven't used C++ for a while, so I might be remembering getline wrong; if someone can confirm that I'd appreciate it.)
Reputation Points: 683
Solved Threads: 53
Posting Virtuoso
Infarction is offline Offline
1,580 posts
since May 2006
Sep 16th, 2006
1

Re: c++ File I/O Help

Click to Expand / Collapse  Quote originally posted by Infarction ...
If you have someIstream >> someString it will read up to the first whitespace. Try getline(infile, name) after you get the 3 integers. (I haven't used C++ for a while, so I might be remembering getline wrong; if someone can confirm that I'd appreciate it.)
I tried it and guess what... IT WORKS! Thanks a lot!

Nick
Reputation Points: 18
Solved Threads: 4
Junior Poster
stupidenator is offline Offline
192 posts
since Mar 2005

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 writing a switch program for class
Next Thread in C++ Forum Timeline: Help with the execution of this program





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


Follow us on Twitter


© 2011 DaniWeb® LLC