fopen,fseek,fread,fclose

来源:互联网 发布:博实股份人工智能 编辑:程序博客网 时间:2024/05/20 19:16
#include<stdio.h>
#include<string.h>


int main()
{
    FILE *file = fopen("text","a+");
    if(NULL == file)
    {
        perror("fopen");
return 1;
    }


    char *str = "helloworld";
    int count1 = fwrite(str, sizeof(str), strlen(str), file);
    if(0 == count1)
    {
        perror("fwrite");
fclose(file);
return 2;
    }


    fseek(file, 0, SEEK_SET);


    char buffer[1024];
    int count2 = fread(buffer,sizeof(char),  1024, file);
    if(0 == count2)
    {
        perror("fread");
fclose(file);
return 3;
    
    }
    
    printf("%s\n",buffer);
    fclose(file);
    return 0;
}