文件编程之C库函数

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值