?
Solved

stream open fails

Posted on 2004-10-24
12
Medium Priority
?
202 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
Comment
Question by:steva
  • 5
  • 4
  • 2
  • +1
12 Comments
 
LVL 15

Expert Comment

by:efn
ID: 12395870
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
ID: 12396561
Your "c:\test.txt" file seems to be read-only.
Right-click it => Properties => uncheck "Read only"
0
 

Author Comment

by:steva
ID: 12402168

>> 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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
LVL 7

Expert Comment

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

Author Comment

by:steva
ID: 12402966
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
ID: 12408648
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
ID: 12413983
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
ID: 12414248
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
ID: 12415575
>>  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:
rstaveley earned 500 total points
ID: 12416164
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
ID: 12416732
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
ID: 12419332
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

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Written by John Humphreys C++ Threading and the POSIX Library This article will cover the basic information that you need to know in order to make use of the POSIX threading library available for C and C++ on UNIX and most Linux systems.   [s…
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

601 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