getc和putc

来源:互联网 发布:网络很卡用英语怎么说 编辑:程序博客网 时间:2024/06/04 23:26

int getc(FILE *Fp)

getc函数返回fp指向的输入流中的下一个字符,如果达到文件尾或出现错误,该函数将返回EOF

int putc(int c,FILE *fp)

该函数将字符c写入到fp指向的文件中,并返回写入的字符,如果发生错误,则返回EOF

类似于getchar和putchar,getc和putc是宏而不是函数

getc:

#include<stdio.h>
#include<stdlib.h>
void main()
{
    FILE *fp;
    fp = fopen("G:\\a.txt", "r");
    int ch;
    ch = getc(fp);
    while (ch!=EOF)
    {
        putchar(ch);
        ch = getc(fp);
    }
    system("pause");
}


putc:

#include<stdio.h>
#include<stdlib.h>
void main()
{
    FILE *fp;
    fp = fopen("G:\\a.txt", "w");
    fputs("hello ,world  hello wold",fp);
    fclose(fp);
    system("pause");
}


fclose()函数

flcose(fp)函数关闭fp执行的文件,必要时刷新缓冲区,对于比较正式的程序,应该检查是否成功关闭文件,如果成功关闭,fclose()函数返回0,否则返回EOF;

if(fclose(fp)!=0)

printf("文件关闭失败");

如果磁盘以满,移动硬盘被移除或出现I/O错误,都会导致fclose()函数失败