文件编程之C库函数

来源:互联网 发布:python list 去重 编辑:程序博客网 时间:2024/05/18 23:29

1、打开流

prototype:

#include <stdio.h>FILE *fopen(const char *restrict pathname, const char restrict type);

fopen 打开路径名为 pathname 的一个指定的文件;
type参数指定该I/O流的读、写方式,ISO C 规定可以有15种不同值。

type 说明 open标志 r或rb 为读而打开 O_RDONLY w或wb 把文件截断为0,或为写而创建 O_WRONLY | O_CREAT | O_TRUNC a或ab 追加,为在文件尾而打开,或为写而创建 O_WRONLY | O_CREAT | O_APPEND r+或r+b或rb+ 为读和写而打开 O_RDWR w+或r+b或wb+ 把文件截断为0,或为读和写而打开 O_RDWR | O_CREAT | O_TRUNC a+或a+b或ab+ 为在文件尾读和写而打开或创建 O_RDWR | O_CREAT | O_APPEND

2、读写字节

prototype:

#include <stdio.h>int fgetc(FILE *fp);int fputc(int c, FILE *fp);

3、读写行

prototype:

#include <stdio.h>char *fgets(char *restrict buf, int n, FILE *restrict fp);char *fputs(char *restrict buf, FILE *restrict fp);

4、定位流

prototype:

#include <stdio.h>int fseek(FILE *fp, long offset, int whence);

demo:

#include <apue.h>#include <stdio.h>#define LINESIZE 16 int main(int argc, const char *argv[]){    FILE *fsrc = fopen("test.c", "r");    if (!fsrc){        fprintf(stderr, "open source file error !");        exit(1);    }    FILE *fdst1 = fopen("1.c",  "w");    if (!fdst1){        fprintf(stderr, "open destination file1 error !");        exit(1);    }    FILE *fdst2 = fopen("2.c",  "w");    if (!fdst2){        fprintf(stderr, "open destination file2 error !");        exit(1);    }    /* 字节读写到文件 */    char c = fgetc(fsrc);    while (c != EOF){        fputc(c, fdst1);        //输出到标准输出端        c = fgetc(fsrc);        //继续读取下一个字符    }    fseek(fsrc, 0, SEEK_SET);   //重新定位到文件头    /* 行读写到文件 */    char buf[LINESIZE];     while (fgets(buf, LINESIZE, fsrc) != NULL){        fputs(buf, fdst2);       //输出文件    }    fclose(fsrc);    fclose(fdst1);    fclose(fdst2);    return 0;}

5、2进制I/O

prototype

ssize_t fread(void *buf, size_t size, size_t nobj, FILE *fp);ssize_t fwrite(const void *buf, size_t size, size_t nobj, FILE *fp); 

返回实际读写的对象数。
注意 : 调用完 fwrite 后,必须用定位函数重新定位到文件头,才能读出数据,因为调用 fwrite 后,文件指针将指向文件尾。


6、格式化I/O

prototype

#include <stdio.h>int fscanf(FILE *fp, const char *format, ...);int fprintf(FILE *fp, const char *format, ...);

demo

#include <apue.h>#include <stdio.h>typedef struct student{    char name[20];    char agent[8];    double score;}stu;int main(int argc, const char *argv[]){    int n, i;    printf("Please input class's number : ");    scanf("%d",&n);    stu class[n];    stu temp[n];    printf("*****************************\n");    printf("*                           *\n");    printf("* Please input student info *\n");    printf("*                           *\n");    printf("*****************************\n");    for (i = 0; i < n; i++){        printf("* %d *\n", i + 1);        printf("name  : ");        fscanf(stdin, "%s",  class[i].name  );        printf("agent : ");        fscanf(stdin, "%s",  class[i].agent );        printf("score : ");        fscanf(stdin, "%lf", &class[i].score);    }    FILE *fp = fopen("3.c", "r+");    fwrite(class, sizeof(stu), 2, fp);    fseek(fp, 0, SEEK_SET);     //重新定位到文件头,因为调用fwrite时,文件指针指向了文件尾    fread(temp, sizeof(stu), 2, fp);    printf("******************************************************\n");    printf("*                                                    *\n");    printf("* Please check if there is any error of student info *\n");    printf("*                                                    *\n");    printf("******************************************************\n");    for (i = 0; i < n; i++){        fprintf(stdout, "* %d * \nname  : %s \nagent : %s \nscore : %lf\n",                i + 1, temp[i].name, temp[i].agent, temp[i].score);    }    fclose(fp);    return 0;}
原创粉丝点击