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:
Include necessary header files:
stdio.h
is included to utilize file handling functions in C.Declare variables:
FILE file;
declares a file pointer andconst char filename = "example.txt";
initializes the filename.Open the file:
file = fopen(filename, "r");
tries to open the file in read mode ("r") and assigns the file pointer tofile
.Check if the file was opened successfully:
if (file != NULL)
checks if the file pointer is not NULL, indicating successful file opening.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.
Close the file pointer:
fclose(file);
closes the file pointer after use.Return 0: Indicates successful completion of the program.