ifstream/ofstream compiler error when using block if

Hi all, I'm new here and pretty new to c++, but I'm getting along well so far. There's lots of help on these boards and most of the time my problems have been resolved by the cplusplus tutorial, other reference books, and search engines, but I just have to post this time. Thanks in advance for any help!

Also, I'd love to have some friends to chat with about development, so:
FackHammer (AIM)
Nino_DeCoy (Yahoo)

Here's the rundown. It seemed more efficient to use an if structure than to paste redundant code, so...

1)If the file exists (is open for input), close and reopen for output in append mode.
2)If not, create it and open for output.
Leaving the if structure, the file should be open for output one way or the other, so
3)Start writing,
but my compiler (wxDevC++ 6.10.2 under Windows 2000 on a Pentium 4) gives me this:

'struct std::ifstream' has no member named 'write'

regarding line 76, the file.write. This code works fine if I put the write inside the if blocks (second bit of code below), but I'm curious why this is happening. Isn't it clear that I'm using ofstream at this point, not ifstream?

I'd just like to understand the issue regardless of having worked around it.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (file.is_open()) //close and reopen for append
        {
            file.close();
            ofstream file("example.bin",ios::out|ios::app|ios::binary);
        }
    else //create
        {
            ofstream file("example.bin",ios::out|ios::trunc|ios::binary)
        }

            //write
            file.write (ADCString.data(), ADCString.length());
            file.close();


workaround:
1
2
3
4
5
6
7
8
9
10
11
12
13
    if (file.is_open())
        {
            file.close();
            ofstream file("example.bin",ios::out|ios::app|ios::binary);
            file.write (ADCString.data(), ADCString.length());
            file.close();
        }
    else
        {
            ofstream file("example.bin",ios::out|ios::trunc|ios::binary);
            file.write (ADCString.data(), ADCString.length());
            file.close();
        }


note: the "trunc" remains because I'm not clear on whether I can omit it once I start messing with other parameters, and how I would. Clarification on that would be nice, too.

(*edit: typo)
Last edited on
Your first code:
1
2
3
4
5
6
7
8
9
10
11
12
if (file.is_open()) // file was declare as ifstream and is used here
{
    file.close();
    ofstream file("example.bin",ios::out|ios::app|ios::binary);//Wrong: redeclaration as ofstream, now you have two objects named 'file' but the ofstream has its scope only within this block
}
else
{
    ofstream file("example.bin",ios::out|ios::trunc|ios::binary)// the same thing
}

file.write (ADCString.data(), ADCString.length());// 'file' is still ifstream, (you can't write) and 'ofstream file' objects are out of scope
file.close();


To solve this: declare 'file' as fstream which can be used both for writing and reading. Instead of redeclare 'file', after calling file.close call file.open with the new flags

(on your second code, you are using the ofstream 'file' in its scope)
Last edited on
Got it! Works flawlessly. Thanks Bazzy!
Topic archived. No new replies allowed.