Thread: File is not created on my harddisk

  1. #1
    Registered User
    Join Date
    Sep 2021
    Posts
    5

    File is not created on my harddisk

    Hi,

    I am learning to program in C but I have encountered a problem. I have the following code:

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main()
    {
       int num;
       FILE *fptr;
    
       // use appropriate location if you are using MacOS or Linux
       fptr = fopen("C:\\program.txt","w");
    
       if(fptr == NULL)
       {
          printf("Error!");
          exit(1);
       }
    
       printf("Enter num: ");
       scanf("%d",&num);
    
       fprintf(fptr,"%d",num);
       fclose(fptr);
    
       return 0;
    }
    When I compile and run this code I get the error message "Error!". So the file "C:\\program.txt" is not created on my harddisk.

    Can someone tell me what is going wrong?

    Thanks

    Ben

  2. #2
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    The basic code works fine under Linux. I don't do Windoze programming anymore, but:
    Code:
    Try changing:
    
       fptr = fopen("C:\\program.txt","w");
    
    to:
    
       fptr = fopen("program.txt","w");
    And see if the file is in the current directory of the C: drive.
    Last edited by rstanley; 09-19-2021 at 12:31 PM.

  3. #3
    Registered User
    Join Date
    Sep 2021
    Posts
    5
    That works.

    Can you tell me why my code doesn't work?

  4. #4
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Which compiler are you using?

    Better answered by a Windoze programmer. I use Linux exclusively.

  5. #5
    Registered User
    Join Date
    Sep 2021
    Posts
    5
    Ok thanks for your help.

    I am using GCC.

  6. #6
    Registered User rstanley's Avatar
    Join Date
    Jun 2014
    Location
    New York, NY
    Posts
    1,106
    Quote Originally Posted by FFX View Post
    Ok thanks for your help.

    I am using GCC.
    What if you try a lowercase 'c'?
    Code:
    fptr = fopen("c:\\program.txt","w");

  7. #7
    Registered User
    Join Date
    Sep 2021
    Posts
    5
    Also doesn't work

  8. #8
    'Allo, 'Allo, Allo
    Join Date
    Apr 2008
    Posts
    639
    It isn't created there because standard user accounts aren't allowed to create files in the root of the C:\ drive.
    But as your app isn't manifested to say it understands this behaviour, it doesn't fail because the file creation is redirected to the virtual store where your user account does have permission and it is created there instead. You can find this directory at C:\Users\%YourUserName%\AppData\Local\VirtualStore
    Even though "it doesn't exist", if you try opening C:\program.txt for reading and read it, it will work fine, since the same redirection will happen.

    If you add a manifest and add the requestedExecutionLevel bit to it, that disables the virtualisation and it'll start failing or the file will appear where you expect, depending on what you put in it. this describes the technical details.

    TL;DR It's Windows being helpful for apps written 20+ years ago / compatability reasons. This is why our Linux friend couldn't comprehend it, they don't bother with that.
    Last edited by adeyblue; 09-19-2021 at 01:56 PM.

  9. #9
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Do you have permission to create a file in the root directory of drive C:? Open a prompt and try:
    Code:
    c:\> echo "hello" > file.txt
    Are you using MSYS? Cygwin?

  10. #10
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    Ahhh... even on Windows you can use '/', instead of '\\', in C, as in "c:/file.txt".

  11. #11
    Registered User
    Join Date
    Feb 2019
    Posts
    1,078
    And... also, you can use "perror()" to show you the error:
    Code:
    f = fopen( "c:/file.txt", "w" );
    if ( f == NULL )
    {
      perror( "fopen" );
      return 1;
    }

  12. #12
    Registered User
    Join Date
    Sep 2021
    Posts
    5
    Quote Originally Posted by flp1969 View Post
    Are you using MSYS? Cygwin?
    I am using MSYS.

    This is indeed an permission problem. Thanks all for your help.

    Ben

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to get Memory Address of a File in HardDisk
    By zacless in forum C Programming
    Replies: 1
    Last Post: 09-12-2013, 11:13 PM
  2. Replies: 5
    Last Post: 11-20-2012, 09:27 PM
  3. Replies: 7
    Last Post: 09-20-2012, 10:06 PM
  4. Help: Finding only Last modified file or Created file
    By lagrz in forum C++ Programming
    Replies: 3
    Last Post: 01-03-2006, 04:49 PM
  5. Looking for a file in the HardDisk
    By BianConiglio in forum C Programming
    Replies: 11
    Last Post: 03-15-2004, 12:52 PM

Tags for this Thread