文件编程

来源:互联网 发布:姚明09年季后赛数据 编辑:程序博客网 时间:2024/06/02 07:28
#include <stdio.h>#include <string.h>int main(){    FILE *file = fopen("./text1", "a+");    if (NULL == file)    {        perror("fopen");        fclose(file);        return 1;    }    char* str = "helloworld";    int fd = fwrite(str, sizeof(char), strlen(str), file);    if (-1 == fd)    {        perror("fwrite");        fclose(file);        return 1;    }    fseek(file, 0, SEEK_SET);//设置文件指针指向的位置为文件起始的位置    char array[1024] = {0};    fd = fread(array, sizeof(char), 1024, file);    if (-1 == fd)    {        perror("fread");        fclose(file);        return 1;    }    printf("%s\n", array);    return 0;}