当前位置 博文首页 > dinglingran的专栏:C语言 关于fgetc 函数

    dinglingran的专栏:C语言 关于fgetc 函数

    作者:[db:作者] 时间:2021-08-09 13:21

    C语言 关于fgetc 函数 ? 百度知道问题 已经答案
    #include "stdafx.h"
    #include "stdio.h"
    int main(int argc, char* argv[])
    {
    FILE*fp,*fp2;
    fp=fopen("D:\\C\\file5\\file.txt","r");
    fp2=fopen("D:\\C\\file5\\file2.txt","w");


    if(fp==NULL)
    printf("cannot open file");
    if(fp2==NULL)
    printf("cannot open file2");
    char ch;

    while(!feof(fp))
    {
    ch=fgetc(fp);
    putchar(ch);
    fputc(ch,fp2);
    }
    fclose(fp);
    fclose(fp2);
    return 0;
    }


    如果while循环里改为:
    ? ? ? ? ? ? ? while(!feof(fp))
    {
    putchar(fgetc(fp));
    fputc(fgetc(fp,fp2);
    }


    输出的就不对了,就缺失字母


    file的内容:
    abcdefg
    hijklmn
    opqrstu
    vwxyz


    每次运行fgetc,都会重新去获得一次字符,你在putchar(fgetc(fp));中的fgetc所获得的字符,和fputc(fgetc(fp,fp2)所获得的字符是不同的两个字符。
    追问
    对奥!怪不得控制台输出的,跟文件输出的,缺的字符不同!!感谢!
    cs