I'm having trouble getting fstream to function properly. I cannot open a text file while using any of the available parameters. My program calls mainprog() and functions correctly if I compile it as follows:

fstream openfile;
openfile("my_file.txt");
if(openfile.fail())
{
      cout<<"Error opening file.";
}
else
{
      mainprog();
}

But if I try to open the file with the in, out, and app parameters...

fstream openfile;
openfile("my_file.txt", fstream::in | fstream::out | fstream::app);
if(openfile.fail())
{
      cout<<"Error opening file.";
}
else
{
      mainprog();
}

... the program fails to open the file and displays the error message. Any suggestions?

Recommended Answers

All 10 Replies

But if I try to open the file with the in, out, and app parameters...

fstream openfile;
openfile("my_file.txt", fstream::in | fstream::out | fstream::app);
if(openfile.fail())
{
      cout<<"Error opening file.";
}
else
{
      mainprog();
}

... the program fails to open the file and displays the error message. Any suggestions?

On the openfile line, are you telling the compiler that it should check the following:

fstream::in (or) fstream::out (or) fstream::app???

if so you are short 1 | for each instance (an OR command is ||)..

if not, then completely disregard all the above and kind of explain what you're trying to accomplish on that line.

That single "|" is the way you pass parameters to file_stream.open() according to my reference. The filename must be in qoutes followed by a comma and then the parameters are fstream::parameter seperated by a single |. Two || are a logical or, not the seperator needed here from what I understand.

On the openfile line, are you telling the compiler that it should check the following:

fstream::in (or) fstream::out (or) fstream::app???

if so you are short 1 | for each instance (an OR command is ||)..

if not, then completely disregard all the above and kind of explain what you're trying to accomplish on that line.

Wrong. The | operator is NOT a boolean operator but instead is a bitwise OR which adds the values together. The op's use of the | operator is correct.

fstream openfile;
openfile("my_file.txt");
if(openfile.fail())
{
      cout<<"Error opening file.";
}
else
{
      mainprog();
}

i'm surprised that this doesn't give you a compilation error. when i compile it i get a straight forward error

fileopenDan.cpp(9) : error C2064: term does not evaluate to a function

and the both the forms work well if you say

openfile.open("my_file.txt", fstream::in|fstream::out|fstream::app);

I've always seen the modes used as ios::in | ios::out | ios::app

I've always seen the modes used as ios::in | ios::out | ios::app

So have I, but see the example here

That's the tutorial I used Ancient Dragon. I don't get a compilation error but rather an error when attempting to open the text file when using any parameters.

I always have problems using fstream too. So I try to avoid it and use ifstream or ofstream instead.

Thanks that makes me glad I'm not the only one!

#include<iostream.h>
#include<fstream.h>
int main()
{
    char ch;
    fstream fio;
    fio.open("my_file.txt", fstream::out | fstream::app);
    if(fio.fail())
    {
          cout<<"Error opening file.";
    }
    else
    {
        cout<<"Opened";
    }
}

this works for me perfectly!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.