| | |
c++ File I/O Help
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi,
I am in need of some help with a small portion of my program.
I have an input file as follows:
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.
Output:
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
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)
10 9 15 program1 11 1 30 important 12 11 10 junk program 14 15 5 other program 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)
#include <fstream> #include <iostream> #include <string> using namespace std; int main(void) { int a,b,c; string name; ifstream infile; infile.open("file.data", ios::in); infile >> a >> b >> c >> name; cout << a << ' ' << b << ' ' << c << ' ' << name << endl; infile >> a >> b >> c >> name; cout << a << ' ' << b << ' ' << c << ' ' << name << endl; infile >> a >> b >> c >> name; cout << a << ' ' << b << ' ' << c << ' ' << name << endl; infile.close(); return 0; }
Output:
C++ Syntax (Toggle Plain Text)
[nnisi@vulcan heap]$ ./a.out 10 9 15 program1 11 1 30 important 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
•
•
•
•
If you havesomeIstream >> someStringit will read up to the first whitespace. Trygetline(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.)
Nick
![]() |
Similar Threads
- connect to text file database (Visual Basic 4 / 5 / 6)
- Importing SQL Script File - Urgent !! (Database Design)
- Syntax for deleting specified file ( in C++) (C++)
- enabling file sharing (Windows NT / 2000 / XP)
- data file help (C)
- 81TB File Server (Networking Hardware Configuration)
Other Threads in the C++ Forum
- Previous Thread: Need help writing a switch program for class
- Next Thread: Help with the execution of this program
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings struct temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






