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:
[HIGHLIGHT="C++"]
#include "stdafx.h"
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char fileName[80];
char buffer[255]; // for user input
cout << "File Name: ";
cin >> 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();
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();
fin.open(fileName); //reassign existing fin object
if (!fin)
{
cout << "Unable to open " << fileName << " for reading.\n";
cout << endl;
system("pause");
return(1);
}
cout << "\nHere's the contents of the file: \n";
char ch;
while (fin.get(ch))
cout <<ch;
cout << "\n****END OF FILE****\n";
fin.close();
cout << endl;
system("pause");
return 0;
}
[/HIGHLIGHT]
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?
problem with ifstream and re-opening
Started by MerakSpielman, Jul 13 2008 07:30 PM
3 replies to this topic
#1
Posted 13 July 2008 - 07:30 PM
|
|
|
#2
Posted 14 July 2008 - 10:25 AM
No ideas? Hm.... Maybe it's because I'm working from an older book -- ©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.
I didn't think the fundamentals had really changed that much, but maybe there's new syntax or something for the ifstream open() function.
#3
Posted 14 July 2008 - 10:48 AM
I seem to recall that you need to clear the stream state in addition to closing the input file.
Or you could make a function and the scope change should do that.
fin.close();
[COLOR="Red"]fin.clear();[/COLOR]
Or you could make a function and the scope change should do that.
#include <iostream>
#include <fstream>
using namespace std;
[COLOR="Red"]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();
}[/COLOR]
int main()
{
char fileName[80];
char buffer[255]; // for user input
cout << "File Name: ";
cin >> fileName;
[COLOR="Red"]foo(fileName);[/COLOR]
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();
[COLOR="Red"]foo(fileName);[/COLOR]
cout << endl;
return 0;
}
#4
Posted 14 July 2008 - 11:27 AM
That did it, thanks!


Sign In
Create Account


Back to top









