Chris Altig
Chris Altig

Reputation: 680

fopen with 'w' flag returns "ERROR_ALREADY_EXISTS"

Okay, so I'm trying to open a file "dump.txt" using fopen with the parameter w. According to MSDN ""w" Opens an empty file for writing. If the given file exists, its contents are destroyed." In which case, fopen("dump.txt","w") should not return ERROR_ALREADY_EXISTS, because that would (and should) not matter with the w flag. I even tried the full path to "dump.txt", but to no avail. What am I doing wrong and how can I fix it?

Upvotes: 0

Views: 324

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 596111

If the file already exists and is being overwritten, fopen() would return a non-NULL FILE* pointer so you can write to the file, and so you should not be doing any error handling to begin with. Don't check for an error code unless fopen() returns NULL. Even then, make sure you are getting the error code from errno and not from GetLastError().

Upvotes: 3

Related Questions