fopen and windows

18,472

Solution 1

I bet you have the Windows "hide extensions" bugfeature turned on. So the file which is really called "file.txt" appears in your Explorer as "file". And if it appeared to be "file.txt" in the Explorer, it would have to be named "file.txt.txt" on the hard drive.

Solution 2

The code itself is fine assuming what it does is what you are wanting.

"r" requires that the file already exists (it wont create one for you) so you need to make sure that the path is correct. Is the desktop path right, not on another drive etc?

The permission error would occur if it was executed by a user other than "ste", or of course if somthing had changed the permissions on that file path some how to prevent access.

Share:
18,472
STE
Author by

STE

Updated on June 04, 2022

Comments

  • STE
    STE almost 2 years

    This is the first time I'm coding C in Windows and a weird bug is driving me crazy.

    I'm trying to open a .txt file using fopen, and it keeps giving me the "file doesn't exist" error (it also gave me the "no permission" error, once).

    My code is as follows (doesn't get any simpler than that):

    FILE *file;
    
    if((file=fopen("C:\\Users\\ste\\Desktop\\file.txt", "r"))==NULL) 
    {
        printf("Cannot open file.\n");
        puts(strerror(errno));
    
        getchar();
        exit(1);
    }
    

    Am I missing something here? Thanks in advance!

  • STE
    STE about 12 years
    well.. The file exists and I'm user ste which means I should be able to access files in my desktop, no ? :) Could it be any permission conflict because I'm building it via the Visual Studio?...
  • Omnibyte
    Omnibyte about 5 years
    a subtle punch to Windows for the "hide extensions" setting, totally agree (+1)