#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0

    Using fopen on C disk


    A somewhat general problem, but here it is:
    I tried to use fopen to create and write a file on the C disk (in the root directory), but it failed. Then, using the same program, I was able to write to a folder in C. Why is this (I have administrator rights), and how can I create a file under the C disk directly?
  2. #2
  3. Contributed User
    Devshed Specialist (4000 - 4499 posts)

    Join Date
    Jun 2005
    Posts
    4,379
    Rep Power
    1871
    Well I suppose the first question is why you would want to be writing anything into C:\ to begin with.

    Because if you screw it up, the next question you'll be asking yourself is "WTF are my OS installation disks?".

    Next, you should provide an actual program, and actual error messages.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <errno.h>
    #include <string.h>
    int main(void)
    {
        FILE *fp = fopen("readonly.txt","w");
        if ( fp == NULL ) {
            fprintf(stderr,"Unable to open:%s\n", strerror(errno));
            return 1;
        }
        fclose(fp);
        return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    Unable to open:Permission denied
    Because "I tried something and it didn't work" is not a useful diagnostic we can help you with.
    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
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2012
    Posts
    14
    Rep Power
    0
    Originally Posted by salem
    Well I suppose the first question is why you would want to be writing anything into C:\ to begin with.

    Because if you screw it up, the next question you'll be asking yourself is "WTF are my OS installation disks?".

    Next, you should provide an actual program, and actual error messages.
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    #include <errno.h>
    #include <string.h>
    int main(void)
    {
        FILE *fp = fopen("C:\\readonly.txt","w");
        if ( fp == NULL ) {
            fprintf(stderr,"Unable to open:%s\n", strerror(errno));
            return 1;
        }
        fclose(fp);
        return 0;
    }
    
    $ gcc foo.c
    $ ./a.out 
    Unable to open:Permission denied
    Because "I tried something and it didn't work" is not a useful diagnostic we can help you with.
    Thanks for the reply. Running your program, I do get the same error. I was just wondering why there is write-protection on the C: disk when (at least some of the folders) within it are not protected. Is there any way of bypassing or removing this protection?

    It just happens that my hard disk was not partitioned into C: and D: disks when I got my laptop (and I didn't do it straight-away><), and pretty soon it became full enough to the point that any partitioning would be awkward.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,578
    Rep Power
    1906
    The problem is not that you only have the c: disk, the "issue" is why you want to write to the root of the drive. There are no need for files to be saved in that place other than the standard system files.

    You didn't specify which OS you are using, but assuming Windows 7 there are several environment variables that can (should?!) be used to save files into, like %APPDATA% and %TEMP%.
    You can get the whole list at:
    http://technet.microsoft.com/en-us/library/dd560744%28v=ws.10%29.aspx

    If you are not using this OS, I will still be surprised if your OS doesn't have similar option.
    Last edited by MrFujin; October 20th, 2012 at 04:59 PM.

IMN logo majestic logo threadwatch logo seochat tools logo