C语言文件操作函数大全(超详细)

2020-01-06 20:18:48王冬梅

_IOFBF 完全无缓冲IO。如果参数buf为NULL指针,则为无缓冲IO。
返回值

29.ungetc(将指定字符写回文件流中)
相关函数 fputc,getchar,getc
表头文件 #include<stdio.h>
定义函数 int ungetc(int c,FILE * stream);
函数说明 ungetc()将参数c字符写回参数stream所指定的文件流。这个写回的字符会由下一个读取文件流的函数取得。
返回值 成功则返回c 字符,若有错误则返回EOF。
复制代码
#include <stdio.h>
#include <stdlib.h>
int main()
{
     FILE *fp = NULL;
     char* str;
     char re; 
     int num = 10;
     str = (char*)malloc(100);
     //snprintf(str, 10,"test: %s", "0123456789012345678");
    // printf("str=%sn", str);
     fp = fopen("/local/test.c","a+");
     if (fp==NULL){
        printf("Fail to open filen");
     }
//     fseek(fp,-1,SEEK_END);
     num = ftell(fp);
     printf("test file long:%dn",num);
     fscanf(fp,"%s",str);
     printf("str = %sn",str);
     printf("test a: %sn",str);
     while ((re=getc(fp))!=EOF){//getc可以用作fgetc用
        printf("%c",re);
     }
     //fread(str,10,10,fp);
     fgets(str,100,fp);
     printf("test a: %sn",str);
     sprintf(str,"xiewei test is:%s", "ABCDEFGHIGKMNI");
     printf("str2=%sn", str);