How to check if file was created in C

2 Answers

0 votes
#include <stdio.h>

int file_was_created(char *path) {
	FILE *file = fopen(path, "w");
	
  	if (file) {
  		fclose(file);
      	return 1;
    }
  	return 0;
}

int main() {
    char path[] = "d:\\development\\test.txt";

    printf("%i\n", file_was_created(path));

    return 0;
}


 
 
/*
run:
 
0
 
*/

 



answered Mar 24, 2021 by avibootz
edited Mar 24, 2021 by avibootz
0 votes
#include <stdio.h>
#include <unistd.h>

int main() {
    char path[] = "d:\\development\\test.txt";

    printf("%i\n", access(path, F_OK ) == 0 );

    return 0;
}


 
 
/*
run:
 
0
 
*/

 



answered Mar 24, 2021 by avibootz
edited Mar 24, 2021 by avibootz

Related questions

1 answer 206 views
1 answer 101 views
1 answer 175 views
2 answers 319 views
1 answer 564 views
...