首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >是否写入.txt文件?

是否写入.txt文件?
EN

Stack Overflow用户
提问于 2012-07-20 14:31:36
回答 3查看 618K关注 0票数 160

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

回答 3

Stack Overflow用户

回答已采纳

发布于 2012-07-20 14:36:46

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 chatacters */
char c = 'A';
fprintf(f, "A character: %c\n", c);

fclose(f);
票数 293
EN

Stack Overflow用户

发布于 2012-07-20 14:50:02

FILE *fp;
char* str = "string";
int x = 10;

fp=fopen("test.txt", "w");
if(fp == NULL)
    exit(-1);
fprintf(fp, "This is a string which is written to a file\n");
fprintf(fp, "The string has %d words and keyword %s\n", x, str);
fclose(fp);
票数 23
EN

Stack Overflow用户

发布于 2012-07-20 14:37:19

那么,你首先需要得到一本关于C语言的好书,并理解这门语言。

FILE *fp;
fp = fopen("c:\\test.txt", "wb");
if(fp == null)
   return;
char x[10]="ABCDEFGHIJ";
fwrite(x, sizeof(x[0]), sizeof(x)/sizeof(x[0]), fp);
fclose(fp);
票数 -4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/11573974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档