c check if file was created

Certainly! Below is an example in C to check if a file was created:

#include <stdio.h>

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

    file = fopen(filename, "r");
    if (file != NULL) {
        printf("File '%s' exists.\n", filename);
        fclose(file);
    } else {
        printf("File '%s' does not exist or cannot be opened.\n", filename);
    }

    return 0;
}

Explanation:

  1. Include necessary header files:stdio.h is included to utilize file handling functions in C.

  2. Declare variables:FILE file; declares a file pointer and const char filename = "example.txt"; initializes the filename.

  3. Open the file:file = fopen(filename, "r"); tries to open the file in read mode ("r") and assigns the file pointer to file.

  4. Check if the file was opened successfully:if (file != NULL) checks if the file pointer is not NULL, indicating successful file opening.

  5. Print appropriate message: If the file exists and was opened successfully, it prints a message stating that the file exists. Otherwise, it indicates that the file doesn’t exist or couldn’t be opened.

  6. Close the file pointer:fclose(file); closes the file pointer after use.

  7. Return 0: Indicates successful completion of the program.