954,487 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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.

//#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;
     }
Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

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:

fin.clear();
fin.close();

You can also avoid mysterious errors like this by using is_open instead of the boolean conversion to check the stream after opening it:

if(!fin.is_open()) //if unable to open for file input
{
  cout << "Unable to open for file input..." << endl;
}

This guarantees that you're testing if the file is open, not if it's in a good state altogether.

Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Matt, don't use deprecated headers and <a href="http://www.gidnetwork.com/b-61.html">system("pause");</a> . They call for bad programming.

~s.o.s~
Failure as a human
Administrator
11,938 posts since Jun 2006
Reputation Points: 3,281
Solved Threads: 734
 

Thanks a lot, that helped and my program works now, although I don't understand what I should be using instead of the iostream.h i tried just iostream and it said cout was undefined...

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

You might have to add the line

using namespace std;


or add a std:: before every cout and cin (etc).

What are you using for a compiler? Sounds like the old Borland thingy!

vegaseat
DaniWeb's Hypocrite
Moderator
5,986 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

I tried that and it still said the same thing. But is any header with the .h a deprecated one? or just the iostream one? Because i tried it with the fstream one just now. And I'm using the latest version of "Bloodshed Dev-C++"

Matt Tacular
Junior Poster
187 posts since Jun 2006
Reputation Points: 10
Solved Threads: 7
 

> 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
#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.

Salem
Posting Sage
Team Colleague
11,531 posts since Dec 2005
Reputation Points: 5,862
Solved Threads: 953
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You