"Permission Denied" error reading file

This is a discussion on "Permission Denied" error reading file within the C++ Programming forums, part of the General Programming Boards category; I'm looking for troubleshooting suggestions or insights. I'm using Microsoft Visual C++ Version 6. In my MFC application, I can ...

  1. #1
    Registered User
    Join Date
    May 2009
    Posts
    4

    "Permission Denied" error reading file

    I'm looking for troubleshooting suggestions or insights. I'm using Microsoft Visual C++ Version 6. In my MFC application, I can read part of a text file with fgets but, if at some point during that, I open an output file, I then get a "Permission Denied" error (using strerror) on the next fgets and cannot read the rest of the text file. Any ideas?

  2. #2
    Registered User carrotcake1029's Avatar
    Join Date
    Apr 2008
    Posts
    398
    Have you verified that you even have write privileges in that directory?

  3. #3
    Registered User
    Join Date
    May 2009
    Posts
    4

    RE: "Permission Denied" error reading file

    carrotcake1029,

    Yes, I write to the directory in other programs without a problem. What's even odder, this "Permission Denied" error occurs on the second pass through this piece of code. On the first pass, opening the output file doesn't cause an error, and I can read to the end of the text file. After I posted, I found that if I do _fcloseall() before opening the text file, then I do not get the error. So if that survives rigorous testing, then it's a viable solution. I close the output file if needed before opening a new one, but apparently something there is amiss. I'm just not sure what.

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Katman View Post
    I'm looking for troubleshooting suggestions or insights. I'm using Microsoft Visual C++ Version 6. In my MFC application, I can read part of a text file with fgets but, if at some point during that, I open an output file, I then get a "Permission Denied" error (using strerror) on the next fgets and cannot read the rest of the text file. Any ideas?
    Is fgets() actually failing, or are you just checking errno and seeing that it's non-zero?

    In other words:

    Code:
    fgets(...);
    if( errno )
       ...
    Because that would be the wrong way to check for failure -- you'll pick up the error code of whatever happened to fail previously, for instance the attempt to open a file for writing.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Registered User
    Join Date
    May 2009
    Posts
    4
    brewbuck,

    The fgets fails. Here's the fgets with strerror added in an attempt to identify the error:

    Code:
          if (NULL == fgets(linebuf, 99, InputFile))
         {	 sprintf(tmp, "\nstrerror says InputFile read failed: %s\n", strerror( errno) );
    	 MessageBox(NULL, tmp, "Error", 0);
          }
          else .....
    If strerror is not returning the fgets error string, what error is it reporting?

  6. #6
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    Quote Originally Posted by Katman View Post
    brewbuck,

    The fgets fails. Here's the fgets with strerror added in an attempt to identify the error:

    Code:
          if (NULL == fgets(linebuf, 99, InputFile))
         {	 sprintf(tmp, "\nstrerror says InputFile read failed: %s\n", strerror( errno) );
    	 MessageBox(NULL, tmp, "Error", 0);
          }
          else .....
    If strerror is not returning the fgets error string, what error is it reporting?
    fgets() might be returning NULL because it is at EOF, not because it experienced an error. Call ferror(InputFile) to see whether there truly was an error on input.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  7. #7
    Registered User
    Join Date
    May 2009
    Posts
    4
    brewbuck,

    Yes, of course I've done ferror already and all that and tested for EOF as well. I think the question has changed somewhat: I open the input file and then close the output file from the first pass before opening a new output file on the second pass. Closing the old output file is a difference between passes 1 and 2. Why does an open input file become "unreadable" after closing an old output file and opening a new output file?

  8. #8
    Registered User
    Join Date
    Apr 2009
    Posts
    34
    Please consider posting the entire code here, so that we can get a better idea.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems passing a file pointer to functions
    By smitchell in forum C Programming
    Replies: 4
    Last Post: 09-30-2008, 03:29 PM
  2. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 02:04 PM
  3. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 08:01 PM
  4. Making a LIB file from a DEF file for a DLL
    By JMPACS in forum C++ Programming
    Replies: 0
    Last Post: 08-02-2003, 09:19 PM
  5. Hmm....help me take a look at this: File Encryptor
    By heljy in forum C Programming
    Replies: 3
    Last Post: 03-23-2002, 10:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21