| | |
Need help reading in a file
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Well, here is the gist of the problem. When I read in a file of integers, if there is a formatting character at the end, the last integer is repeated. For instance, in the code, it is reading i01.dat. If that file reads as follows:
2
3
4
5
with the newline character (or tab character, or whatever) at the end, then the five is printed twice when I cout the input even though I tried to use a break for the .eof() or .fail(). I have tried to use an if statement to test for \\n or \\t (i.e. if (i != \\n)) but the compiler does not understand this so I am obviously not testing properly. Please help me understand this. Here's what I've got so far.
2
3
4
5
with the newline character (or tab character, or whatever) at the end, then the five is printed twice when I cout the input even though I tried to use a break for the .eof() or .fail(). I have tried to use an if statement to test for \\n or \\t (i.e. if (i != \\n)) but the compiler does not understand this so I am obviously not testing properly. Please help me understand this. Here's what I've got so far.
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <string> #include <iomanip> #include <cstdlib> using namespace std; struct CommandLineException { CommandLineException(int max, int actual) { cout << endl << "Too many command line arguments." << endl; cout << "A maximum of " << max << " arguments are permitted." << endl; cout << actual << " arguments were entered." << endl; } }; struct FileException { FileException(char* fn) { cout << endl << "File " << fn << " could not be opened." << endl; } }; int main(int argc, char* argv[]) { try { char ifn[255], ofn[255]; switch(argc) { case 1: cout << "Enter the input file name. "; cin >> ifn; cout << "Enter the output file name. "; cin >> ofn; break; case 2: strcpy(ifn, argv[1]); cout << "Enter the output file name. "; cin >> ofn; break; case 3: strcpy(ifn, argv[1]); strcpy(ofn, argv[2]); break; default: throw CommandLineException(2, argc - 1); break; } ifstream i(ifn); if(!i) throw FileException(ifn); ofstream o(ofn); if(!o) throw FileException(ofn); int testmsg; while (true) { if (i.eof()) break; i >> testmsg; cout << testmsg << " "; } o.close(); i.close(); } catch(...) { cout << "Program terminated." << endl; exit(EXIT_FAILURE); } return 0; }
•
•
Join Date: Mar 2005
Posts: 36
Reputation:
Solved Threads: 1
C++ Syntax (Toggle Plain Text)
int testmsg; while (true) { if (i.eof()) break; i >> testmsg; cout << testmsg << " "; }
Try
C++ Syntax (Toggle Plain Text)
while(!eof)
C++ Syntax (Toggle Plain Text)
do { i >> testmsg; cout << testmsg << endl; }while(testmsg);
I tried this one
and it tells me "invalid use of memeber (did you forget the '&' ?)'
"in argument to unary !"
I also tried this with
and it tells me that this is " 'eof' undeclared (first use this function)"
I tried this one also and it works but with the same problem I originally described. Thanks for the response though.
C++ Syntax (Toggle Plain Text)
while (!i.eof) {blah blah}
"in argument to unary !"
I also tried this with
C++ Syntax (Toggle Plain Text)
while (!eof)
•
•
•
•
Originally Posted by Raven11
or
C++ Syntax (Toggle Plain Text)
do { i >> testmsg; cout << testmsg << endl; }while(testmsg);
Dave Sinkula reports something like that and a smart solution in thread:
http://www.daniweb.com/techtalkforum...ad19956-2.html
It could be compiler dependent, since I was not able to repeat the problem.
http://www.daniweb.com/techtalkforum...ad19956-2.html
It could be compiler dependent, since I was not able to repeat the problem.
May 'the Google' be with you!
![]() |
Similar Threads
- problems with reading random access line from a file (C++)
- First year assigment on reading file, sorting and outputting invoice (C++)
- Error Message Concerning Reading File From A Drive (C++)
- Reading in a file. (Java)
- loop to create arrays when reading a file (Java)
- URGENT - Reading from txt file into a 2 dimension array (Java)
- reading a file into code (Java)
- reading and printing a file to screen in C (C)
Other Threads in the C++ Forum
- Previous Thread: stl vector - can you delete by position?
- Next Thread: Need help understanding what to do
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays assignment based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer 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 rpg simple sorting string strings template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






