Lost Password?


Go Back   CodeCall Programming Forum > Software Development > C and C++

C and C++ C and C++ forum for discussing all forms of C except for C#. These languages are powerful low level languages used for creating Operating Systems, Device Drivers, compilers and much more.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 07-13-2008, 11:30 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default problem with ifstream and re-opening

I'm following some exercises in a book to figure out how to work with files, and I thought I was doing pretty good... but for some reason I can't get my program to open a file.

The lesson is teaching me how to open a file in Append mode using ios:app. It seems to be working properly, as in it's appending the new information to the file and I can see it when I open the file, but it refuses to re-open the file within the program.

Here's the code:

C++ Code:
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <fstream>
  4.  
  5. using namespace std;
  6.  
  7.  
  8. int main() 
  9. {
  10.     char fileName[80];
  11.     char buffer[255]; // for user input
  12.     cout << "File Name: ";
  13.     cin >> fileName;
  14.  
  15.     ifstream fin(fileName);
  16.     if (fin)
  17.     {
  18.         cout << "Current file contents:\n";
  19.         char ch;
  20.         while (fin.get(ch))
  21.             cout << ch;
  22.         cout << "\n****END OF FILE****\n";
  23.     }
  24.     fin.close();
  25.  
  26.     cout << "\nOpening " << fileName << " in append mode...\n";
  27.  
  28.     ofstream fout(fileName,ios::app);
  29.     if (!fout)
  30.     {
  31.         cout << "Unable to open " << fileName << " for appending.\n";
  32.  
  33.         return(1);
  34.     }
  35.  
  36.     cout << "\nEnter text for the file: ";
  37.     cin.ignore(1,'n');
  38.     cin.getline(buffer,255);
  39.     fout << buffer << "\n";
  40.     fout.close();
  41.  
  42.     fin.open(fileName); //reassign existing fin object
  43.     if (!fin)
  44.     {
  45.         cout << "Unable to open " << fileName << " for reading.\n";
  46.         cout << endl;
  47.         system("pause");
  48.         return(1);
  49.     }
  50.     cout << "\nHere's the contents of the file: \n";
  51.     char ch;
  52.     while (fin.get(ch))
  53.         cout <<ch;
  54.     cout << "\n****END OF FILE****\n";
  55.     fin.close();
  56.    
  57.    
  58.     cout << endl;
  59.     system("pause");
  60.  
  61.     return 0;
  62. }

First it opens the file using ifstream, and if the filename already exists it displays the file's contents (line 15-24).

Then, (line 26-40) it opens the file in append mode and lets the user put in a string of text. This is appended to the file correctly.

The problem comes in the next section, lines 42-55. Specifically on line 42. According to my book, I don't need to use ifstream. fin is already declared (back on line 15) so fin.open(fileName) should open up the file for reading.

But every time I try it, the "if (!fin)" loop is activated and I get the error message saying the file couldn't be opened and the program terminates.

(I added "system("PAUSE")" lines to keep the console box open so I could see the output. Not the best method, I know, but it's quick and easy.)

So why can't I reopen my file for reading? What's the problem here?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote

Sponsored Links
  #2 (permalink)  
Old 07-14-2008, 02:25 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default Re: problem with ifstream and re-opening

No ideas? Hm.... Maybe it's because I'm working from an older book -- (c)1999.

I didn't think the fundamentals had really changed that much, but maybe there's new syntax or something for the ifstream open() function.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 07-14-2008, 02:48 PM
dcs dcs is offline
Programming Expert
 
Join Date: Mar 2008
Posts: 371
Rep Power: 6
dcs has a spectacular aura aboutdcs has a spectacular aura about
Default Re: problem with ifstream and re-opening

I seem to recall that you need to clear the stream state in addition to closing the input file.

Code:
    fin.close();
    fin.clear();
Or you could make a function and the scope change should do that.
Code:
#include <iostream>
#include <fstream>
 
using namespace std;

void foo(const char *fileName)
{
   ifstream fin(fileName);
   if (fin)
   {
       cout << "Current file contents:\n";
       char ch;
       while (fin.get(ch))
           cout << ch;
       cout << "\n****END OF FILE****\n";
   }
   fin.close();
}

int main() 
{
    char fileName[80];
    char buffer[255]; // for user input
    cout << "File Name: ";
    cin >> fileName;
 
    foo(fileName);
    cout << "\nOpening " << fileName << " in append mode...\n";
 
    ofstream fout(fileName,ios::app);
    if (!fout)
    {
        cout << "Unable to open " << fileName << " for appending.\n";
 
        return(1);
    }
 
    cout << "\nEnter text for the file: ";
    cin.ignore(1,'n');
    cin.getline(buffer,255);
    fout << buffer << "\n";
    fout.close();
 
    foo(fileName);
   
    cout << endl;
 
    return 0;
}
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 07-14-2008, 03:27 PM
MerakSpielman MerakSpielman is offline
Newbie
 
Join Date: Jan 2008
Posts: 20
Rep Power: 4
MerakSpielman is on a distinguished road
Default Re: problem with ifstream and re-opening

That did it, thanks!
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
i can fix my code. problem with pointer. fire exit C and C++ 8 10-02-2007 12:53 AM
Weird problem with code, functions when not suppose to Murph-s C and C++ 0 05-14-2007 08:11 PM
A small problem in the output The_Master C and C++ 3 12-13-2006 01:04 PM


All times are GMT -5. The time now is 11:07 AM.

Contest Stats

WingedPanther ........ 2753.6
Xav ........ 2704
Brandon W ........ 1702.32
John ........ 1207.73
marwex89 ........ 1175.24
morefood2001 ........ 966.05
dcs ........ 655.75
Steve.L ........ 475.59
orjan ........ 418.58
Aereshaa ........ 383.54

Contest Rules

CodeCall Goal

Goal: 100,000 Posts
Complete: 101%


Complete - Celebrate!

Ads