1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | #include <stdio.h>
#include <stdlib.h>
void write_file(char const* filename, char const* contents) {
FILE* fp = fopen(filename, "w");
for(int i = 0; contents[i] != '\0'; i++) {
char char_to_write = contents[i];
fputc(char_to_write, fp); // write one character
}
fclose(fp);
}
int main(int argc, char* argv[]) {
char const contents[] = "ABC"; // string literal ("▒") implies an ending '\0'.
// IOW, the '\0' is added by the compiler automatically
// to a string literal (i.e., with double quotes).
//
// Null terminator ('\0') only has special meaning with
// C string library functions, e.g., strlen(…),
// strcat(…), printf("…"), and so forth.
//
// String literal is a char* and cannot have bytes >127.
//
// String literal can't be used for uchar with bytes
// >127.
char const contents[] = { 'A', 'B', 'C', '\0' };
write_file("a.txt", contents);
return EXIT_SUCCESS;
}
|
© Copyright 2022 Alexander J. Quinn This content is protected and may not be shared, uploaded, or distributed.