| | |
Doesn't open for file input successfully.... why?
![]() |
**I know the code looks big, but it's a very small isolated section at the bottom (starting at line 164) that's giving me the problem.**
When this program is run, when I try to open the file I just appended some text to, it doesn't open... can anyone spot why? Thanks....
-Matt
p.s. Yes this is a program I made after following a tutorial, incase you're wondering.
When this program is run, when I try to open the file I just appended some text to, it doesn't open... can anyone spot why? Thanks....
-Matt
p.s. Yes this is a program I made after following a tutorial, incase you're wondering.
C++ Syntax (Toggle Plain Text)
//#include <iostream.h> <--No need for this because fstream includes it #include <iomanip.h> #include <fstream.h> int main() { char a, b, c, d; char ch; //four character arrays char stringOne[256]; char stringTwo[256]; char stringThree[256]; char stringFour[] = "This is a string of characters!"; cout << "Enter 3 letters: "; cin.get(a).get(b).get(c).get(d); cout << "A: " << a << endl << "B: " << b << endl << "C: " << c << endl << "D: " << d << endl; //for the terminating character thing cout << "Enter string one: "; cin.get(stringOne,256); cout << "stringOne: " << stringOne << endl << endl; cout << "Enter string two: "; cin >> stringTwo; cout << "stringTwo: " << stringTwo << endl << endl; /* If the user enters something with a space and then more after the space up in string two, then the remainder after the white space in string two will be left to linger in the input buffer and then will be immediatly put into string three below, you won't even get a chance to enter anything in string three */ cout << "Enter string three: "; cin.getline(stringThree,256); cout << "stringThree: " << stringThree << endl << endl; cout << "Try again..." << endl << "Enter string two: "; cin >> stringTwo; cout << "stringTwo: " << stringTwo << endl << endl; cin.ignore(255,'\n'); cout << "And now you actually get to enter string three: "; cin.getline(stringThree,256); cout << "stringThree: " << stringThree << endl << endl; /* The following section of code demonstrates the put() function */ cout.put('H').put('e').put('l').put('l').put('o').put('\n'); /* peek() and putback() */ cout << "Enter a phrase: "; while ( cin.get(ch) ) { if (ch == 'e') { cin.putback('E'); } else { cout << ch; } while (cin.peek() == ' ') { cin.ignore(1,' '); } if (ch == '\n') { break; } } /* The write() function */ int fullLength = strlen(stringFour); int tooShort = fullLength -4; int tooLong = fullLength + 6; cout << "using write(), with a parameter that's correct:" << endl; cout.write(stringFour,fullLength) << endl << endl; cout << "using write(), with a parameter that's too short:" << endl; cout.write(stringFour,tooShort) << endl << endl; cout << "using write(), with a parameter that's too long:" << endl; cout.write(stringFour,tooLong) << endl << endl; /* The width() function */ cout << "Width function:"; cout.width(25); cout << "The paramter was 25" << endl << endl; cout << "Width function again:"; cout.width(25); cout << "Paramter also 25\n"; cout << "Width only goes to the NEXT output." << endl << endl; cout << "Width function AGAIN: "; cout.width(4); cout << "Paramter was 4, way too short, but that's the same as just enough." << endl << endl; /* The setf() function */ const int number = 234; cout << "The number is: " << number << endl; cout << "That number, " << number << ", in hex is " << hex << number << endl; cout << "The hex number, with it's base shown: "; cout.setf(ios::showbase); cout << hex << number << endl; cout << "The number, formated with 10 width: "; cout.width(10); cout << hex << number << endl; cout << "Number with alignment 'left': "; cout.width(10); cout.setf(ios::left); cout << hex << number << endl; cout << "Number with alignment 'internal': "; cout.width(10); cout.setf(ios::internal); cout << hex << number << endl; cout << "Using concatenated setw() to set width to 10:" << setw(10) << hex << number << endl << endl; /* File reading and writing */ char fileName[80]; char buffer[255]; //for user input cout << "Enter filename: "; cin >> fileName; ofstream fout(fileName); // open for writing fout << "This was written to file in the background..." << endl; cout << "Enter text for file writing: "; cin.ignore(1,'\n'); // eat the newline after the file name cin.getline(buffer,255); // get the user's input fout << buffer << "\n"; // and write it to the file fout.close(); // close the file, ready for reopen ifstream fin(fileName); cout << "Here is what's currently in the file: " << endl; char ch2; while(fin.get(ch2)) { cout << ch2; } cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl; fin.close(); //just being neat and tidy /* file writing in append mode */ cout << "Opening file in append mode: " << endl; fout.open(fileName,ios::app); //reasign existing fout object if(!fout) //if unable to open file for output.... same as if(fout.fail()) { cout << "Was unable to open file for output..." << endl; } cout << "Enter more text for the previous file: " << endl; cin.ignore(1,'\n'); cin.getline(buffer,255); fout << buffer << "\n"; fout.close(); fin.open(fileName); //reasign existing fin object if(!fin) //if unable to open for file input { cout << "Unable to open for file input..." << endl; } cout << "Here's the contents of the file: " << endl; while(fin.get(ch2)) { cout << ch2; } cout << " -=-=-=-=-= End of file contents =-=-=-=-=- " << endl << endl; fin.close(); /* The fill() function */ cout << "Using fill() to add astericks wherever whitespace is added from width(): " << endl; cout << "Start --> "; cout.width(25); cout.fill('*'); cout << " <-- End" << endl << endl; system("PAUSE"); return 0; }
Your problem is that opening and closing files doesn't change the stream state. When you read fin to end-of-file, closing the stream doesn't change the eofbit. You need to clear the stream state as well as close the file if you plan on using fin to read from a file again:
You can also avoid mysterious errors like this by using is_open instead of the boolean conversion to check the stream after opening it:
This guarantees that you're testing if the file is open, not if it's in a good state altogether.
C++ Syntax (Toggle Plain Text)
fin.clear(); fin.close();
C++ Syntax (Toggle Plain Text)
if(!fin.is_open()) //if unable to open for file input { cout << "Unable to open for file input..." << endl; }
Last edited by Narue; Jun 4th, 2007 at 11:51 am.
I'm here to prove you wrong.
Matt, don't use deprecated headers and
system("pause");. They call for bad programming. I don't accept change; I don't deserve to live.
You might have to add the line
or add a std:: before every cout and cin (etc).
What are you using for a compiler? Sounds like the old Borland thingy!
C++ Syntax (Toggle Plain Text)
using namespace std;
What are you using for a compiler? Sounds like the old Borland thingy!
Last edited by vegaseat; Jun 4th, 2007 at 1:44 pm.
May 'the Google' be with you!
> i tried just iostream and it said cout was undefined...
Well you also need to learn about namespaces as well.
A crude "fix" is to convert
to
Better use would be only list the things which interest you.
And even better, avoid "using" altogether and be totally explicit in what type of cout you mean.
The more precise you can be, the fewer problems you'll have when it comes to using code which has several namespaces.
> But is any header with the .h a deprecated one? or just the iostream one?
All the ones declared by ANSI have lost the .h suffix, and now make use of the "std" namespace.
The latest dev-c++ should be fine.
Well you also need to learn about namespaces as well.
A crude "fix" is to convert
#include <iostream.h>to
#include <iostream>
using namespace std;Better use would be only list the things which interest you.
#include <iostream>
using std::cout;
using std::cin;And even better, avoid "using" altogether and be totally explicit in what type of cout you mean.
#include <iostream>
// then later in the code proper
std::cout << "Enter 3 letters: ";The more precise you can be, the fewer problems you'll have when it comes to using code which has several namespaces.
> But is any header with the .h a deprecated one? or just the iostream one?
All the ones declared by ANSI have lost the .h suffix, and now make use of the "std" namespace.
The latest dev-c++ should be fine.
![]() |
Similar Threads
- How to open txt file (Visual Basic 4 / 5 / 6)
- Open Excel file from Visual Basic (Visual Basic 4 / 5 / 6)
- file input don't know where to start (C++)
- open an https file (Python)
- file open code in VB (Visual Basic 4 / 5 / 6)
- File input. (C++)
- how to open vaw file (Java)
- 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: Help with C++ code using structs and arrays
| Thread Tools | Search this Thread |
api array based binary bitmap business c++ c/c++ char class classes code coding commentinghelp compile console conversion count decide delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error faq file forms fstream function functions game givemetehcodez graph gui hash homeworkhelp homeworkhelper iamthwee ifpug ifstream incrementoperators infinite input int integer java lib linkedlist linker loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem proficiency program programming project python random read recursion reference rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






