| | |
Help with File Reading loop
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 0
Okay, I'm trying to do my homework, but my code isn't doing what I want it to. What I'm trying to do is have the user enter a file name and then the program will output the number of words in the file. It works fine the first time through the loop, but when prompted again, the file never opens correctly. Here is my code:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cassert> using namespace std; int main() { int numberOfWords; string fileName; string wordString; ifstream infile; cout << "File to process (type quit to exit): "; cin >> fileName; while(fileName != "quit") { infile.open(fileName.c_str()); assert(infile); infile >> wordString; for(numberOfWords = 0; infile; numberOfWords++) { infile >> wordString; } infile.close(); cout << fileName << " has " << numberOfWords << " words." << endl << endl; cout << "File to process (type quit to exit): "; cin >> fileName; } }
Last edited by alc6379; Mar 1st, 2005 at 11:22 am.
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 0
I have six different files that all work the first time through the loop. I've tried re-ordering the loop stuff, but nothing seems to work. I then tried infile.close() but that didn't do anything. Please don't write the code for me... maybe a suggestion on what to do would be best. I really like this class. This is the first time I've run into something I cannot fix myself.
Thanks.
Thanks.
DISREGUARD, You are using iostream and I'm not sure how that works with strings.
You can't compare strings the way you do.
I think that should solve your problem.
You can't compare strings the way you do.
C++ Syntax (Toggle Plain Text)
while (strcmp (filename, "quit") != 0)
I think that should solve your problem.
Last edited by Tight_Coder_Ex; Feb 28th, 2005 at 9:57 pm. Reason: Failed to notice you are using iostream
>You can't compare strings the way you do.
Sure you can. The string class overloads the relational operators. Though <string> needs to be included.
>the file never opens correctly
Try this:
>assert(infile);
No. assert is designed as a debugging tool, not as a run-time error handling mechanism. Failure to open a file is not considered a programming error, it's an input error in this case. So you should be using a conditional statement:
I know it's subtle, but a program error and a programming error are two completely different things and need to be dealt with in different ways.
Sure you can. The string class overloads the relational operators. Though <string> needs to be included.
>the file never opens correctly
Try this:
C++ Syntax (Toggle Plain Text)
#include <iostream> #include <fstream> #include <cassert> #include <string> using namespace std; int main() { int numberOfWords; string fileName; string wordString; cout << "File to process (type quit to exit): "; cin >> fileName; while(fileName != "quit") { ifstream infile(fileName.c_str()); assert(infile); infile >> wordString; for(numberOfWords = 0; infile; numberOfWords++) { infile >> wordString; } cout << fileName << " has " << numberOfWords << " words." << endl << endl; cout << "File to process (type quit to exit): "; cin >> fileName; } }
No. assert is designed as a debugging tool, not as a run-time error handling mechanism. Failure to open a file is not considered a programming error, it's an input error in this case. So you should be using a conditional statement:
C++ Syntax (Toggle Plain Text)
if (!in) { cerr<<"Failure to open file"<<endl; // Terminate here or handle the error }
I'm here to prove you wrong.
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 0
I just read (http://www.eg.bucknell.edu/~cs204/Fa...++-strings.pdf) that you can use the comparisson opperators to compare strings and it returns a boolean value.
•
•
Join Date: Feb 2005
Posts: 15
Reputation:
Solved Threads: 0
I treid putting #include <string> but it didn't change anything. The assert thing comes from my teacher. He wanted us to use that instead of an if statement. Don't ask me why. I think an if statement is more clear during the run time phase of a program, though an assert statement may look cleaner in the actual code. Any more suggestions?
![]() |
Similar Threads
- file reading help (C++)
- Client/Server sockets and reading a file (C++)
- trouble with counting letter from file, please help. (C++)
Other Threads in the C++ Forum
- Previous Thread: Error when tring to run program PLEasE Help
- Next Thread: Class that represents sets of integers
| Thread Tools | Search this Thread |
Tag cloud for C++
api application array arrays based beginner binary bmp c++ c/c++ calculator char char* class classes code compile compiler console conversion convert count data delete deploy dll download dynamic dynamiccharacterarray encryption error file format forms fstream function functions game givemetehcodez graph gui homeworkhelp iamthwee ifstream input int java lib library linkedlist linker list loop looping loops map math matrix memory microsoft newbie news number numbertoword output pointer problem program programming project python random read recursion recursive reference rpg simple sorting string strings temperature template templates test text text-file tree url variable vector video visual visualstudio void win32 windows winsock wordfrequency wxwidgets






