//-std=c99 -Wall -Wextra -Wpedantic -Wshadow
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int main()
{
FILE *fp;
char fileName[] = "test_text_file.txt";
fp = fopen(fileName, "r");
if (fp)
{
printf("WARNING: file \"%s\" already exists ! Overwrite it ? [Y/N]\n", fileName);
char c = toupper(getchar());
if ( c != 'Y' )
{
printf("Not overwriting. Bye !\n");
fclose(fp);
return 0;
}
fclose(fp);
}
fp = fopen(fileName, "w");
if (!fp)
{
printf("ERROR: can't open file \"%s\" for writing !\n", fileName);
return -1;
}
fprintf(fp,"%s\n", "some text");
fclose(fp);
printf("INFO: a \"%s\" file has been written (overwritten). Bye !\n", fileName);
return 0;
}
Comments