c++ File I/O Help

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

c++ File I/O Help

 
1
  #1
Sep 16th, 2006
Hi,

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

I have an input file as follows:

  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.
  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:
  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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,580
Reputation: Infarction has a spectacular aura about Infarction has a spectacular aura about Infarction has a spectacular aura about 
Solved Threads: 52
Infarction's Avatar
Infarction Infarction is offline Offline
Battle Programmer

Re: c++ File I/O Help

 
1
  #2
Sep 16th, 2006
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.)
Reply With Quote Quick reply to this message  
Join Date: Mar 2005
Posts: 192
Reputation: stupidenator is an unknown quantity at this point 
Solved Threads: 4
stupidenator's Avatar
stupidenator stupidenator is offline Offline
Junior Poster

Re: c++ File I/O Help

 
1
  #3
Sep 16th, 2006
Originally Posted by Infarction View Post
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
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C++ Forum


Views: 1555 | Replies: 2
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC