028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

字符输入、输出

/*从键盘输入一串字符到d:\123.txt文件中,然后输出到屏幕上。*/
#include
main()
{
    FILE *fp;                                   //文件指针
    fp=fopen("d:\\123.txt","w");                //写入文本文件
    if(fp==NULL)
    {
        printf("Can not open this file!\n");    //文件不存在
        exit(0);
    }
    char ch;
    ch=getchar();
    while(ch!='#')                              //循环输入直到"#"结束
    {
        fputc(ch,fp);                           //输出一个字符到fp指定的文件
        ch=getchar();                           //读入一个字符到ch
    }
    fclose(fp);                                 //文件关闭
    fp=fopen("d:\\123.txt","r");                //读文本文件
    if(fp==NULL)
    {
        printf("Can not open this file!\n");    //文件不存在
        exit(0);
    }
    while(!feof(fp))                            //如果不是文件尾,则继续。
    {
        ch=fgetc(fp);                           //从文件读入一个字符到ch
        putchar(ch);                            //输出字符
    }
    fclose(fp);                                 //文件关闭
}

当前名称:字符输入、输出
URL网址:http://www.tsicrk.com/article/goospj.html

其他资讯

让你的专属顾问为你服务

2.1039s