如何使用C.创建.txt文件。这只是创建一个文件 [英] How to create a .txt file using C.Here it is simply creating a file

查看:90
本文介绍了如何使用C.创建.txt文件。这只是创建一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

void main()
{

    FILE *text;
    char file_name[13];
    char string,ch;
    printf("Please enter the file name");
    gets(file_name);
    text = fopen(file_name,"w");
    system("CLS");
    printf("Enter your data here");

    do
    {
        string = getchar();
        fputc(string,text);

    }

    while(string!='^');
    {
        fclose(text);
    }

    getch();


}

推荐答案

文本文件只是一个可以读取的文件通过文本编辑器:没有什么特别之处。通常,它们将具有扩展名.txt,但不是必需的。由于您的代码一次从用户读取一个字符并将其直接写入输出文件,它将创建一个文本文件 - 如果用户指定.txt扩展名,则双击将使用记事本打开它(或类似的)自动。但如果他不这样做,文本编辑仍然可以阅读它。



BTW:不要打电话给 char 变量 string - 它意味着一个以空字符结尾的字符串数组而不是一个字符。



并整理出来你的括号!

快速浏览使你认为:

A "Text file" is just a file that can be read by a text editor: there is nothing special about it. Normally, they will have the extension ".txt" but it isn't required. Since your code reads a char at a time from the user and writes that directly into the output file, it will create a "text file" - if the user specifies a ".txt" extension, then double clicking will open it with Notepad (or similar) automatically. But if he doesn't, text editors can still read it.

BTW: Don't call char variables string - it implies an null-terminated array of strings rather than a single character.

And sort out your brackets!
A quick glance makes you think that this:
while(string!='^');
{
    fclose(text);
}

是一个自己的循环...

使用:

is a loop on it's own...
Use:

do
{
    string = getchar();
    fputc(string,text);
} while(string!='^');
fclose(text);

相反。





先生一切正常我上面的问题很好,但当我尝试使用符号关闭文件时^这也与我创建的文件一起打印。我现在应该做什么来消除这个符号。请先帮助我这个先生。



好​​吧,你看看循环,就是你告诉它要做的事情!

因为你总是打印这个角色然后测试你是否应该退出,最后一个字符将打印到文件。

所以相反,在循环内测试,并使用 break 退出:

Instead.


"Sir everything is working fine in my above question but when I try to close the file using the symbol ^ This also gets printed along with my file which I created.What should I do now to eliminate this symbol.Kindly help me with this Sir."

Well, it you look at the loop, that is what you told it to do!
Because you always print the character then test if you should exit, the final character will be printed to the file.
So instead, test inside the loop, and use break to exit:

do
{
    string = getchar();
    if (string!='^') break;
    fputc(string,text);
} while(1 == 1);
fclose(text);


这篇关于如何使用C.创建.txt文件。这只是创建一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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