stream open fails

Does anyone know why the second open below fails? To make the second open work you must do it as
                 file_out.open(file_name, ios::in | ios::out);


file_out.open(file_name, ios::out);  doesn't even work.

The test.txt file exists and is just three lines of text. I'm compiling with VC++ 6.0.

Thanks for any ideas
Steve



#include <fstream.h>
#include <stdio.h>



int main(int argc, char* argv[], char *envp[])
{

      ifstream file_in;
      ofstream file_out;

      char file_name[80] = "c:\\test.txt";

      file_in.open(file_name);
      printf("\n%s %s for input", file_in.is_open()? "Opened" : "Could not open", file_name);
      file_in.close();

      file_out.open(file_name);
      printf("\n%s %s for output\n\n", file_out.is_open()? "Opened" : "Could not open", file_name);
      file_out.close();

      return(0);

}
stevaAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

efnCommented:
I tried it with the same compiler and it didn't fail.

You might try using the standard header file fstream instead of the obsolete fstream.h.
BenMorelCommented:
Your "c:\test.txt" file seems to be read-only.
Right-click it => Properties => uncheck "Read only"
stevaAuthor Commented:

>> You might try using the standard header file fstream instead of the obsolete fstream.h.

I tried this (had to also add 'using namespace std;') but get exactly the same thing.

>> Your "c:\test.txt" file seems to be read-only.

Nope.  ReadOnly is already unchecked.  for test.txt.

test.txt is a notepad file with three lines:
line 0
line 1
line 2


Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

BenMorelCommented:
Did you create this text file with another user that you're working with ?
stevaAuthor Commented:
No.  I'm the only one on the machine and I'm Administrator.  Plus, notepad has no trouble opening and writing the file for me.

Steve
rstaveleyCommented:
What happens if you have file_in and file_out in separate scopes?

e.g.

--------8<--------
int main(int argc, char* argv[], char *envp[])
{
     char file_name[80] = "c:\\test.txt";
{ // Scope 1
     ifstream file_in;
 

     file_in.open(file_name);
     printf("\n%s %s for input", file_in.is_open()? "Opened" : "Could not open", file_name);
     file_in.close();
}
{ // Scope 2
     ofstream file_out;

     file_out.open(file_name);
     printf("\n%s %s for output\n\n", file_out.is_open()? "Opened" : "Could not open", file_name);
     file_out.close();
}
     return(0);

}
--------8<--------
stevaAuthor Commented:
rstaveley,

Putting the read and write in different scopes, as you suggested, has no effect. The write open still fails.

Thanks for the thought, though.

Steve
rstaveleyCommented:
What do you get if you test the access permissions between the two scopes using _access as indicated at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vccore98/HTML/_crt__access.2c_._waccess.asp ?

If you create separate programs, do you still get the open error?
stevaAuthor Commented:
>>  If you create separate programs, do you still get the open error?

It turns out that I don't even need the open_for_input to fail. I now have that ifdef'd out out and still get the failure just trying to open for output.

>> What do you get if you test the access permissions between the two scopes using _access?

All four modes return 0.  For a sanity check I set the file properties for read-only and then I see _access return -1 for write permission and  -1 for read and write permission.

The code is now exactly as below and running it displays the output shown below the listing.


#include <fstream>
#include <stdio.h>
#include <io.h>

using namespace std;

int acc_ret;
int mode;


int main(int argc, char* argv[], char *envp[])
{

            char file_name[80] = "c:\\test.txt";
#ifdef OMIT      
            { // Scope 1
            ifstream file_in;

            file_in.open(file_name);
            printf("\n%s %s for input", file_in.is_open()? "Opened" : "Could not open", file_name);
            file_in.close();
      }
#endif

      { // Scope 2
            ofstream file_out;
      
            mode=0;
            acc_ret = _access(file_name, mode);
            printf("\n_access returns %d for mode %d on \"%s\"", acc_ret, mode, file_name);

            mode=2;
            acc_ret = _access(file_name, mode);
            printf("\n_access returns %d for mode %d on \"%s\"", acc_ret, mode, file_name);

            mode=4;
            acc_ret = _access(file_name, mode);
            printf("\n_access returns %d for mode %d on \"%s\"", acc_ret, mode, file_name);

            mode=6;
            acc_ret = _access(file_name, mode);
            printf("\n_access returns %d for mode %d on \"%s\"", acc_ret, mode, file_name);

            file_out.open(file_name);
            printf("\n%s %s for output\n\n", file_out.is_open()? "Opened" : "Could not open", file_name);
            file_out.close();
      }


      return(0);

}


/////////////////////////////////////////////////////////

The output from running the above is:

_access returns 0 for mode 0 on "c:\test.txt"
_access returns 0 for mode 2 on "c:\test.txt"
_access returns 0 for mode 4 on "c:\test.txt"
_access returns 0 for mode 6 on "c:\test.txt"
Could not open c:\test.txt for output
rstaveleyCommented:
If you type at the command prompt...

    attrib c:\test.txt

What do you see?

Try then typing:

   attrib -r c:\test.txt

Then try your program again.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
stevaAuthor Commented:
Ah!  attrib shows that the file was both "hidden" and a "system" file.  This makes sense since it started life by being copied from boot.ini and then I changed the text.  If I create a fresh test.txt with notepad the open works fine.  Further experimentation shows that the "hidden" or "system" attribute blocks the file_out.open - unless you include the ios::in | ios::out flags. It is, in fact, boot.ini that I need to work with so I will need to include the ios::in | ios:: out flags, apparently.

So where is this "feature" of file_out.open documented? :)

Thanks for your help.
Steve
rstaveleyCommented:
The system and hidden file attributes are OS-specific (MD-DOS legacy) and standard C++ tools do not provide tools to work with them. It seems pretty quirky that ORing the in and out flags does the trick. You ought really to use Windows API calls - e.g. see http://msdn.microsoft.com/library/default.asp?url=/library/en-us/fileio/base/retrieving_and_changing_file_attributes.asp
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.