Problem creating file in C -- possible permission error?

This is a discussion on Problem creating file in C -- possible permission error? within the C Programming forums, part of the General Programming Boards category; My code system includes an extensive library file written by someone else, which contains a number of function definitions etc. ...

  1. #1
    Registered User
    Join Date
    Jun 2010
    Posts
    67

    Problem creating file in C -- possible permission error?

    My code system includes an extensive library file written by someone else, which contains a number of function definitions etc. In the past, I've never had problems calling on this code. However, I have recently had to change PCs, and the new PC that I'm working on (which has Windows 7, compared to my previous PC that had XP) is resulting in errors that had never been present when using the identical code on my earlier PC.

    Specifically, the straightforward code is

    Code:
    strcpy(filename, fileprefix);
    strcat(filename, "outfile.out");
          
    if ((refmap = fopen(filename,"w")) == NULL) {	   
          fprintf(logfile,"Could not create outfile.out file.\n");
          strcpy(error_msg,"Could not create outfile.out file.");
          fclose(logfile);
          return 1;
    }

    There should be nothing wrong with the above code. However, when I try to run my system, I invariably get the error message, "Could not create outfile.out".
    I'm thinking that this may be a permissions issue.

    When I right-click on my working folder, it shows that the folder & its subfolders are read-only. When I uncheck that box so that these *aren't* read-only and click Apply, the process seems to complete successfully, but then if I check this folder's properties again, it's back to being read-only.

    My apologies that this isn't strictly a C programming question, but has anyone encountered such a problem? I've been told that it doesn't matter if this read-only box is checked, because I can still work with my files; however, this is the only explanation I have for why I wouldn't be able to create the outfile.out file. Any ideas about how to get my files to not be read-only? When I try to adjust the properties, I *am* already working in my Administrator account...

    Any suggestions are welcome. Thanks in advance.
    Last edited by CodeKate; 11-02-2010 at 10:44 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    2,753
    Do you have permissions to change the permissions of the folder?

    You may have to login as "administrator" or whatever Windows 7 calls the super-user.

    Jim

  3. #3
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,540
    Calling strerror() or perror() would at least tell you something about WHY you couldn't write to the file.

    Also, print out the actual full path. Certainly past Vista, trying to write outside your own "c:\users\me" area is going to come with strings attached.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  4. #4
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Thanks for your responses. Some updates:

    -- When I switch modes and enter Administrator mode, it's unfortunately still the same outcome as before: even after I apply my changes to remove the read-only property from my folder, and it appears to complete successfully, checking the folder's properties reveals that it's *still* read-only...

    -- I'm finding that I'm no longer able to compile my code (I now get a 'cannot open file: permission denied' error where I had once been able to compile with no errors)... maybe switching to the Administrator mode changed something? I would *think* that switching to Adminstrator would give me *more* capabilities, not fewer...

    I wonder if some/all of these problems are Windows 7-specific.
    Last edited by CodeKate; 11-02-2010 at 11:36 AM.

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Yes this is a permissions error of sorts...

    Windows 7 is totally non-receptive to data files being created in the "C:\program files" heirarchy. You will trigger the UAC and all kinds of permission errors doing that.

    The simple solution is to move your code into your user folders... Click your name on the start menu, this will open your home folder... simply make a folder "Programming" or such and move all your code to it, leaving the compilers in the program files folders.

    Second, you will find that a lot of code will trigger the UAC unless you include a manifest in your program's resources...
    Code:
    <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
    <assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0">
      <assemblyIdentity type="win32"
                        name="AutoLogon"
                        version="1.0.0.0"
                        processorArchitecture="X86" />
      <description>
        Auto logon tool
      </description>
      <dependency>
        <dependentAssembly>
          <assemblyIdentity type="win32"
                            name="Microsoft.Windows.Common-Controls"
                            version="6.0.0.0"
                            processorArchitecture="X86"
                            publicKeyToken="6595b64144ccf1df"
                            language="*" />
        </dependentAssembly>
      </dependency>
      <trustInfo xmlns="urn:schemas-microsoft-com:asm.v3">
        <security>
          <requestedPrivileges>
            <requestedExecutionLevel  level="asInvoker" 
                                      uiAccess="false" /> 
          </requestedPrivileges>
        </security>
      </trustInfo>
    </assembly>
    Be very careful about editing this... change only the program name and description. Change nothing else.

    Also Win7 gets real snarky about trying to create data files in the program folder under "program files". It will redirect them to a per-user data folder which is exposed by the %appdata% environment variable. The result of this "virtualization" is that when your programs go to open files, they will fail, since the files aren't where it thought it saved them. The new right way is to create folders under the user's home folder or in "documents" for your data files...

    Finally, locate your ide, compiler and linker in the "program fiiles" heirarchy and right click on them... select Properties then Compatibility and check the "Run As Administrator" box on each.

    Yeah, I know... "And exactly what the $#%&#& were they thinking???"


    Hopefully this will fix your problem.
    Last edited by CommonTater; 11-02-2010 at 11:46 AM.

  6. #6
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Thanks for your detailed help, CommonTater. I am seriously considering switching back to XP; all of the debugging steps you mentioned just don't seem like they'll be worth the effort -- i.e., these debugging steps may allow my code to work for now, but how can I predict which additional errors or incompatibilities Windows 7 will cause as I continue developing my code? I am using Matlab R2007b & several related pieces of Mathworks software; mex-files; dlls; C code; a C-mex s-function;... I would not look forward to continual debugging as Windows 7 finds new nuances of my developing system to be incompatible with. Is this a legitimate concern that I have?

    Would you agree with my inclination to switch back to XP? Can you foresee any downsides to doing so? Thanks a lot for your input.
    Last edited by CodeKate; 11-02-2010 at 12:01 PM.

  7. #7
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CodeKate View Post
    Thanks for your detailed help, CommonTater. I am seriously considering switching back to XP; all of the debugging steps you mentioned just don't seem like they'll be worth the effort -- i.e., these debugging steps may allow my code to work for now, but how can I predict which additional errors or incompatibilities Windows 7 will cause as I continue developing my code?

    Would you agree with my inclination to switch back to XP? Can you foresee any downsides to doing so? Thanks a lot for your input.
    If your code is strictly for your own consumption, it doesn't matter a whit what OS you use.

    However, if any of it is going to leave your tender protection, you will need to make it as broadly compatible as possible... That means manifests, moving data folders, learning how to work with UAC and a few other things.

    Those are not debugging steps I laid out... that's how Win7 works and that's what it expects. Doing things the Win7 way is backward compatible with XP... but the translation does not carry the other way... a lot of XP and win2000 code has to be revised to work properly on Win7.

    Should you go back to XP... well, no, because soon enough everyone's going to want code for Win7 and you're going to have to learn the new stuff eventually.

  8. #8
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    Thanks for your input. I'll have to think about whether to switch back to XP; this code is my dissertation project, so it will be used only by me, to collect this one-time project data. I don't envision it being used after this particular project ends...

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by CodeKate View Post
    Thanks for your input. I'll have to think about whether to switch back to XP; this code is my dissertation project, so it will be used only by me, to collect this one-time project data. I don't envision it being used after this particular project ends...
    No worries... and best of luck with your projects.

  10. #10
    Registered User
    Join Date
    Jun 2010
    Posts
    67
    >> Windows 7 is totally non-receptive to data files being created in the "C:\program files" heirarchy.

    CommonTater, I was reminded that I'm actually not working in C:\Program Files; rather, my hard drive has been partitioned into 2 segments, and I'm working on my D: drive (with my C: drive being reserved for program files).

    Does this change any aspect(s) of your long post above? Is any of what you posted still relevant to me, or as far as you know, "should" I theoretically be ok? Another expert I am consulting is confident that Windows 7 should be fully compatible with my code.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    If you are working on a separte data drive it may be an ownership problem...

    Click "computer" on your start menu, right click the drive ... Is there an entry for "Take Ownership"... If so use that to gain full possession of the drive. If not, you can do the same thing in properties -> security -> advanced -> Owner.

    Now, please be aware that my earlier advice is not aimed at compiling your programs... it is aimed at the programs themselves. There are new rules about how software has to behave on Win7... For example: a lot of stuff from pre-XP still creates it's data files in the Program Files directory. This is now verboten, as I explained earlier. These are not changes to be made to your IDE/Compiler... they are changes needed in your application programs.

    As for your other expert... I'm going to do something you won't like... If you are getting conflicting advice, it would be in your best interest to "pick one" and stick with that. Doesn't matter if it's me or not, it's about avoiding the confusion of conflicting advice.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  2. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  3. C++ compilation issues
    By Rupan in forum C++ Programming
    Replies: 1
    Last Post: 08-22-2005, 05:45 AM
  4. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 10:52 PM
  5. Linking error
    By DockyD in forum C++ Programming
    Replies: 10
    Last Post: 01-20-2003, 04:27 AM

Tags for this Thread


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