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?

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);

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.

#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!

This question has already been answered. Start a new discussion instead.