Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

stream open fails

Posted on 2004-10-24
12
Medium Priority
?
201 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Suggested Courses

618 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