| | |
File input.
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
Hey guys and gals.
I'm struggling yet again to get my file input working. I have a text file containing:
John 19 smith 15 03 1986
Billy 15 Nomate 19 07 1990
...
etc
I'm needing to get a line in at a time but also store the variables in different types.
I've got this and it prints out "john" which is fair enough. But how do i get it to start doing the '19' and smith part along with the rest.
Since I'll have varibles called
char firstname[20];
char surname[20];
int age;
...etc all numbers need storing differently. (ie in each own int or char varible).
Make sence I hope so.
Thanks,
John
I'm struggling yet again to get my file input working. I have a text file containing:
John 19 smith 15 03 1986
Billy 15 Nomate 19 07 1990
...
etc
I'm needing to get a line in at a time but also store the variables in different types.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cstring> using namespace std; int main() { ifstream infile; char buffer[25]; char firstname[25]; infile.open("emp.txt"); if (infile.fail()) { cerr << "error " <<endl; exit(1); } char ch; while((ch = infile.get()) !=EOF && (ch!=' ')) cout << ch; return 0; }
I've got this and it prints out "john" which is fair enough. But how do i get it to start doing the '19' and smith part along with the rest.
Since I'll have varibles called
char firstname[20];
char surname[20];
int age;
...etc all numbers need storing differently. (ie in each own int or char varible).
Make sence I hope so.
Thanks,
John
you are going about this the hard way. This is a c++ program -- stop using C character arrays :mad:
You don't have to read the bloody file one character at a time. fstream has operators and functions that will read entire words or numbers into your program for you. All you have to do is learn how to use them. For example
That will read an integer directly into the variable.
or you can put all the above all on one line
and use infile.is_open() to test that the file was opened ok, infile.fail() may not work as expected.
You don't have to read the bloody file one character at a time. fstream has operators and functions that will read entire words or numbers into your program for you. All you have to do is learn how to use them. For example
C++ Syntax (Toggle Plain Text)
int n; infile >> n;
C++ Syntax (Toggle Plain Text)
std::string last_name, first_name, mi; infile >> last_name >> first_name >> mi;
or you can put all the above all on one line
C++ Syntax (Toggle Plain Text)
std::string first_name, last_name; int age; infile >> first_name >> age >> last_name;
and use infile.is_open() to test that the file was opened ok, infile.fail() may not work as expected.
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
Hello again, I've created a struct not allowed to create a class, and pop'd in there Char firstname[20];
so the struc is called record;
so i create a 'new' (dynamically) record called (p) and do:
infile >> p->firstname;
...
cout << p->firstname;
However at runtime the program crashes, anyone help us out?
so the struc is called record;
so i create a 'new' (dynamically) record called (p) and do:
infile >> p->firstname;
...
cout << p->firstname;
However at runtime the program crashes, anyone help us out?
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Ancient Dragon
Our crystal ball is out of order today. Guess you'll have to post your new code :p
C++ Syntax (Toggle Plain Text)
ifstream infile; infile.open("info.txt"); if(infile.is_open()) { infile >> temp->name; } else { cerr << "Failure in opening file"<< endl; exit (1); } cout << temp->name; }
since the file contains more than one line how'd do i go through reading 1 line at a time like this?
you should normally use a loop to read all the lines of a file. For example
C++ Syntax (Toggle Plain Text)
while( infile >> temp->name ) cout << temp->name << endl;
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Ancient Dragon
you should normally use a loop to read all the lines of a file. For example
C++ Syntax (Toggle Plain Text)
while( infile >> temp->name ) cout << temp->name << endl;
C++ Syntax (Toggle Plain Text)
while(infile >> temp->name >> temp->age) { insert(); // call the function }
the while loop no longer calls insert... and if im wrong it wont do it for each line?
amd this causes and infinitive loop:
[php]
while(!infile.eof())
{
cout << "********************" << endl;
infile >> temp->name >> temp->age;
frontinsert(); // insert it into the node.
}
[/php]
•
•
•
•
Originally Posted by Acidburn
thats not going to work, I'm putting them into a storage and then calling a function.
C++ Syntax (Toggle Plain Text)
while(infile >> temp->name >> temp->age) { insert(); // call the function }
the while loop no longer calls insert... and if im wrong it wont do it for each line?
•
•
Join Date: Dec 2004
Posts: 489
Reputation:
Solved Threads: 5
•
•
•
•
Originally Posted by Ancient Dragon
you are wrong -- that loop will read each line until end-of-file is reached (assuming each line contains name and age). Call whatever functions you want within that loop. You can pass temp to insert() if insert() needs the information.
![]() |
Similar Threads
- Doesn't open for file input successfully.... why? (C++)
- file input problems (with windows?) (Java)
- Reading file input into an array (C++)
- Storing file input to an array? (C)
- Reading in file input up to newline (C++)
- file input don't know where to start (C++)
- Getting all data from an input and output file (C++)
- vc++ mfc-i can't make getline work with a string for file input (C++)
Other Threads in the C++ Forum
- Previous Thread: help!
- Next Thread: Is there a way to tokenize an array of strings
| Thread Tools | Search this Thread |
api array based binary bitmap c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






