c check if file exists

To check if a file exists in C, you can use the fopen() function to try opening the file in read mode. If the file does not exist, fopen() will return a NULL pointer. Here's an example:

#include <stdio.h>

int main() {
    FILE *file;
    const char *filename = "example.txt";

    file = fopen(filename, "r");
    if (file == NULL) {
        printf("File does not exist.\n");
    } else {
        printf("File exists.\n");
        fclose(file);
    }

    return 0;
}

In this example, we declare a FILE pointer named file and a string variable filename to store the name of the file we want to check. We then use fopen() to try opening the file in read mode by passing the filename and "r" as arguments. If fopen() returns NULL, it means the file does not exist and we print a corresponding message. Otherwise, the file exists, and we print a different message before closing the file using fclose().

You can replace "example.txt" with the path and name of the file you want to check. Remember to escape backslashes if you're using a Windows file path.