60 lines
1.6 KiB
C
60 lines
1.6 KiB
C
#include <stdio.h>
|
|
|
|
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 write_all_in_one(char *filename, const char *text){
|
|
|
|
FILE *file = fopen(filename, "w");
|
|
if(!file)return 1;
|
|
|
|
fprintf(file, "%s\n", text);
|
|
|
|
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
|
|
|
|
// does_exist.txt contains: "Hello World!"
|
|
a = write_all_in_one("does_exist.txt", "What a good day!");
|
|
// does_exist.txt contains: "What a good day!"
|
|
|
|
// still_not_yet_exist.txt... does not exist.
|
|
b = write_all_in_one("still_not_yet_exist.txt", "Now I exist");
|
|
// still_not_yet_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;
|
|
}
|
|
*/
|