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?
 
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.
0
 
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.
0
 
BenMorelCommented:
Your "c:\test.txt" file seems to be read-only.
Right-click it => Properties => uncheck "Read only"
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
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


0
 
BenMorelCommented:
Did you create this text file with another user that you're working with ?
0
 
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
0
 
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<--------
0
 
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
0
 
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?
0
 
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
0
 
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
0
 
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
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.