#include int write_one_by_one(char *filename, const char *text){ FILE *file = fopen(filename, "w"); if(!file) return 1; int index = 0; while(*(text + index) != '\0'){ fputc(*(text + index ), file); index ++; } //fputc(*text, file); fputc('\0', file); fclose(file); return 0; } int main() { // does_exist.txt contains: "something which will be replaced" int a = write_one_by_one("does_exist.txt", "Hello World!"); // does_exist.txt contains: "Hello World!" // does_not_exist.txt... does not exist. int b = write_one_by_one("does_not_exist.txt", "Now I exist"); // does_not_exist.txt... now exists and contains: "Now I exist" printf("%d %d\n", a, b); // 0 0 // The opening should succeed as it creates the file if needed // However it can fail if the file's access is forbidden return 0; }