| | |
C++ I/O Files
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
C++ Syntax (Toggle Plain Text)
int main() { string name; cout << "Please enter the filename of the encoded message:"; getline(cin,name); ifstream infile; infile.open(name.c_str()); if (infile.fail()) { cout << name << " fail to open." << endl; exit(1); } cout << name << endl; cout << " File successfully opened" << endl; string buffer; getline(infile, buffer); while ( !infile.eof() ) { cout << buffer << endl; } return 0; }
Last edited by Narue; Nov 17th, 2008 at 10:01 pm. Reason: added code tags
This is how you read a file using getline:
C++ Syntax (Toggle Plain Text)
while ( getline ( infile, buffer ) ) { // Process the line }
New members chased away this month: 3
•
•
Join Date: Oct 2008
Posts: 9
Reputation:
Solved Threads: 0
•
•
•
•
This is how you read a file using getline:
C++ Syntax (Toggle Plain Text)
while ( getline ( infile, buffer ) ) { // Process the line }
I don't understand that can u post it in my code?
•
•
Join Date: Jan 2008
Posts: 3,836
Reputation:
Solved Threads: 503
C++ Syntax (Toggle Plain Text)
getline(infile, buffer); while ( !infile.eof() ) { cout << buffer << endl; }
This is an infinite loop. Put the getline statement inside the while loop. If you put it where Narue says to put it, you can get rid of the eof part.
C++ Syntax (Toggle Plain Text)
while ( getline(infile, buffer) ) { cout << buffer << endl; }
![]() |
Similar Threads
- hjsplit doenst work for avi files? (Windows NT / 2000 / XP)
- "Save Target As.." isn't working in IE6 (Web Browsers)
- Does Samba send deleted files to a recycle bin? (*nix Software)
- problems wid cpp files (C++)
- Dia, .PDB files (C++)
- Cannot transfer files from one hardrive to another =[ please help (Windows NT / 2000 / XP)
- FTP files (Geeks' Lounge)
Other Threads in the C++ Forum
- Previous Thread: Extract a word from a text file
- Next Thread: GCC Exception handling w/ threads
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete developer display dll dynamiccharacterarray email encryption error file format forms fstream function functions game generator getline givemetehcodez graph iamthwee ifstream image input int java lib list loop looping loops map math matrix memory multidimensional multiple newbie news node number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






