silly ofstream/ifstream issue

Hey there,

I've been trying to read up on I/O but none of the sites/books that I've looked at really deal with using the same file as both an ifstream and ofstream. I thought the conversion would be automatic, but that seems to not be the case with my compiler and the simple method that I've tried to convert them doesn't seem to work, leaving me puzzled...

My code is basically as below:
#include "nofun.h"
#include <iostream>

using namespace std;

int main()
{
ifstream infile("ifile1.txt");

ifstream ninfile("ifile2.txt");

ofstream outfile("ofile1.txt");

ofstream newfile("ofile2.txt");


myFun1(infile, ninfile, outfile);

outfile.close();
ninfile.close();

ifstream tinfile("ofile1.txt");
if ( ! ninfile )
{
cerr << "Error: Cannot open ofile1.txt!" << endl;
return 1;
}

myFun2(infile,tinfile,newfile);
}

I keep getting the last error - Error: Cannot open ofile1.txt!

This is weird because the file definitely exists, I created it in the beginning of the program and edited it via myFun1. The file is in my project directory and I can open it and look at the present text. At first I figured that maybe the issue was that it was still in the open state due to it being handled as an ofstream file above when passed to myFun1. I tried putting outfile.close() to close it, and then declare a new ifstream file - tinfile - which reads this same document. That didn't seem to work, however, I still get the same error.

It's driving me nuts that I'm spending so long trying to figure this out, I feel that the solution must be really simple but just out of my grasp. If anyone here might be able to point out the flaw in how I'm doing this, I'd be very appreciative.

Thanks,

miked983
oh man, well I was write, it was very simple....

ifstream tinfile("ofile1.txt")
if ( ! ninfile)

.... doh!
Topic archived. No new replies allowed.