c - 写入 .txt 文件?

如何将一小段文本写入 .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);

https://stackoverflow.com/questions/11573974/

相关文章:

linux - 了解 Linux/proc/pid/maps 或/proc/self/maps

python - 在 Pandas 中将 float 转换为整数?

python - 如何在 Python 中直接获取字典键作为变量(而不是通过从值中搜索)?

python - 如何编写符合 PEP8 的超长字符串并防止 E501

c - 使用 gcc 命令行从 .c 文件构建 .so 文件

mysql - 有没有办法只安装mysql客户端(Linux)?

python - 如何使用 Pyplot 在所有子图之上设置一个主标题?

linux - 将主机端口转发到 docker 容器

python - 在python中将一个列表插入另一个列表的语法是什么?

python - 如果在 Python 中为 None,是否有返回默认值的简写?