Fopen and windows

This is the first time I encode C on Windows, and a weird bug is driving me crazy.

I try to open a .txt file with fopen and it continues to give me an error "file does not exist" (it also gave me an error "without permission", once).

My code is as follows (it doesn't get easier):

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? Thanks in advance!

+4
source share
3 answers

I am sure that the "hide Windows extensions" function is turned on. Thus, a file that is really called "file.txt" appears in your explorer as a "file." And if it turned out to be "file.txt" in Explorer, it would have to be called "file.txt.txt" on the hard drive.

+14
source

The code itself is good if it does what you want.

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

A permission error will occur if it was executed by a user other than "ste", or, of course, if something changed the permissions on this file path as a way to prevent access.

+1
source

I use Windows 7. When I run a sample program from the IDE (for example, CTRL + F10 in Code :: Blocks), it doesn’t work - with or without an extension (for example, "file" or ".txt file").

However, if I run the compiled program from the console (not related to the IDE) , it works , regardless of whether the file has an extension or not.

-one
source

Source: https://habr.com/ru/post/1393783/


All Articles