如何将一小段文本写入 .txt
文件中?
我已经在谷歌上搜索了超过 3-4 个小时,但不知道该怎么做。
fwrite();
有这么多参数,我不知道如何使用它。
当您只想将名称和一些数字写入 .txt
文件时,最简单的函数是什么?
char name;
int number;
FILE *f;
f = fopen("contacts.pcl", "a");
printf("\nNew contact name: ");
scanf("%s", &name);
printf("New contact number: ");
scanf("%i", &number);
fprintf(f, "%c\n[ %d ]\n\n", name, number);
fclose(f);
最佳答案
FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
printf("Error opening file!\n");
exit(1);
}
/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s\n", text);
/* print integers and floats */
int i = 1;
float pi= 3.1415927;
fprintf(f, "Integer: %d, float: %f\n", i, pi);
/* printing single characters */
char c = 'A';
fprintf(f, "A character: %c\n", c);
fclose(f);
关于c - 写入.txt 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40554941/