Solved

stream open fails

Posted on 2004-10-24
199 Views
Last Modified: 2010-04-01
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);

}
0
Question by:steva
    12 Comments
     
    LVL 15

    Expert Comment

    by:efn
    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
     
    LVL 7

    Expert Comment

    by:BenMorel
    Your "c:\test.txt" file seems to be read-only.
    Right-click it => Properties => uncheck "Read only"
    0
     

    Author Comment

    by:steva

    >> 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
     
    LVL 7

    Expert Comment

    by:BenMorel
    Did you create this text file with another user that you're working with ?
    0
     

    Author Comment

    by:steva
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    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
     

    Author Comment

    by:steva
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    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
     

    Author Comment

    by:steva
    >>  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
     
    LVL 17

    Accepted Solution

    by:
    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
     

    Author Comment

    by:steva
    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
     
    LVL 17

    Expert Comment

    by:rstaveley
    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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
    Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
    The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
    The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

    846 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    5 Experts available now in Live!

    Get 1:1 Help Now