写入 .txt 文件? [英] Write to .txt file?

查看:31
本文介绍了写入 .txt 文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一小段文本写入.txt 文件?我已经在谷歌上搜索了超过 3-4 个小时,但不知道怎么做.

How can I write a little piece of text into a .txt file? I've been Googling for over 3-4 hours, but can't find out how to do it.

fwrite(); 参数太多,不知道怎么用

fwrite(); has so many arguments, and I don't know how to use it.

当您只想将名称和几个数字写入 .txt 文件时,最容易使用的函数是什么?

What's the easiest function to use when you only want to write a name and a few numbers to a .txt file?

char name;
int  number;
FILE *f;
f = fopen("contacts.pcl", "a");

printf("
New contact name: ");
scanf("%s", &name);
printf("New contact number: ");
scanf("%i", &number);

fprintf(f, "%c
[ %d ]

", name, number);
fclose(f);

推荐答案

FILE *f = fopen("file.txt", "w");
if (f == NULL)
{
    printf("Error opening file!
");
    exit(1);
}

/* print some text */
const char *text = "Write this to the file";
fprintf(f, "Some text: %s
", text);

/* print integers and floats */
int i = 1;
float pi= 3.1415927;
fprintf(f, "Integer: %d, float: %f
", i, pi);

/* printing single chatacters */
char c = 'A';
fprintf(f, "A character: %c
", c);

fclose(f);

这篇关于写入 .txt 文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆