Cannot open Files and getting error code 0x80070002 on C

0

Hi i had a sample code for reading and writing on a txt file but visual studio wont read the code and keep crashing and giving same error 0x80070002 (error happens in bot "w" and "r" formats) here is the code

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    FILE* fp;
    if ((fp = fopen("C:\Users\39351\source\repos\exercise\prova.txt", "r")) == NULL)
        exit(1); 
    else {
        int c;
        while ((c = fgetc(fp)) != EOF) putchar(c);
        fclose(fp);
    }
}

no matter the code when i use the fopen command even fopen_s it gives this error can someone help?

c
asked on Stack Overflow Jul 5, 2020 by milad naderi

2 Answers

2

The backslash (\) character introduces an escape sequence in C string literals, so it's trying to process the \U, \3, \s, \r, \e, and \p as escape sequences. For example, \r translates to a carriage return character. To fix this you need to double up the backslash characters:

#include <stdio.h>
#include <stdlib.h>
int main(void) {
    FILE* fp;
    if ((fp = fopen("C:\\Users\\39351\\source\\repos\\exercise\\prova.txt", "r")) == NULL)
        exit(1); 
    else {
        int c;
        while ((c = fgetc(fp)) != EOF) putchar(c);
        fclose(fp);
    }
}
1

try to use '/' istead '\'

you can change the location of the file, it may be a permission failure

answered on Stack Overflow Jul 5, 2020 by Amaury Ribeiro • edited Jul 5, 2020 by Bob Jarvis - Reinstate Monica

User contributions licensed under CC BY-SA 3.0