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

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

问题描述

我怎么能写一点一段文字到 .TXT 文件?
我一直在谷歌上搜索超过3-4小时,但不能找出如何做到这一点。

的fwrite(); 有这么多的争论,我不知道如何使用它

什么是最简单的功能,使用时你只想要写一个名字和一些数字到 .TXT 文件?

编辑:增加了我的一块code的

 字符名称;
    INT编号;
    FILE * F;
    F =的fopen(contacts.pcl,一个);    的printf(\\ n新联系人姓名:);
    scanf函数(%S,&安培;名);
    的printf(新的联系号码:);
    scanf函数(%i的,&安培;号码);
    fprintf中(F,%C \\ N [%D]。\\ n \\ n,名称,编号);
    FCLOSE(F);


解决方案

  FILE * F =的fopen(file.txt的,W);
如果(F == NULL)
{
    的printf(打开文件时出错\\ n!);
    出口(1);
}/ *打印一些文本* /
为const char *文字=写这篇文章到文件;
fprintf中(F,一些文本:%S \\ n,文本);/ *打印整数和浮* /
INT I = 1;
浮PY = 3.1415927;
fprintf中(F,整型数:%d,浮动:%F \\ N,我,PY);/ *打印单chatacters * /
焦C ='A';
fprintf中(F,A字:%C \\ n,C);FCLOSE(F);

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(); has so many arguments, and I don't know how to use it.

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

Edit: Added a piece of my code.

    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 py = 3.1415927;
fprintf(f, "Integer: %d, float: %f\n", i, py);

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

fclose(f);

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

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