| | |
Input file data
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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)
#include <iostream> #include <fstream> #include <strings> using namespace std; int main() { fstream readFile; readFile.open("Input.txt"); string sample; readfile<<sample; cout<<"Here is a line: "<<sample; return 0;
•
•
Join Date: Aug 2008
Posts: 77
Reputation:
Solved Threads: 16
It should be since you are getting input, not outputting.
C++ Syntax (Toggle Plain Text)
readfile >> sample;
•
•
Join Date: Jun 2008
Posts: 89
Reputation:
Solved Threads: 7
Well.... If you want to use the getline function, here's a simple program to test that:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> using namespace std; int main() { int line_number = 1; string line; ifstream myfile("test.txt"); if(myfile.is_open()) { while (!myfile.eof()) { getline(myfile, line); cout << "line " << line_number << " --> " << line << endl; line_number++; } } else { cout << "The file cannot be opened!!" << endl; } system("PAUSE"); }
> while (!myfile.eof())
http://faq.cprogramming.com/cgi-bin/...&id=1043284351
Use
Try it with a small text file (say 5 lines) and note the difference.
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.
![]() |
Similar Threads
- txt data into 2d Array (C++)
- Reading a input file (C)
- Error with taking input from a file (C++)
- Reading an input file as a class memeber function (C++)
- Please I need help formatting an output file and sorting the names! (C++)
- I need help on STRUCTURES and on input file (C)
- Lines are being skipped in the input file. (Visual Basic 4 / 5 / 6)
Other Threads in the C++ Forum
- Previous Thread: Need help
- Next Thread: editing source files over FTP
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion convert count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game generator givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int java lib linkedlist linker list loop looping loops map math matrix memory multiple news 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 visual visualstudio win32 windows winsock wordfrequency wxwidgets






