文件FILE写入与读取

一,介绍fgetc,fputc是一个一个字符串读取的

/*************************************************************************
    > File Name:
    > Author: songli
    > QQ:2734030745
    > Mail: 15850774503@163.com
    > Created Time:
 ************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, char *argv[])
{
    printf("请您输入文件的路径:\n");

    char filename1[50], filename2[50], ch;

    scanf("%s", filename1);
    FILE *in = fopen(filename1, "rb");   //以读的方式打开filename1
    if (!in)
        return -1;
    printf("请您输入文件的路径:\n");
    scanf("%s", filename2);
    FILE *out = fopen(filename2, "wb");//以写的方式打开filename2
    if (!out)
        return -1;

    while ((ch = fgetc(in)) != EOF)
    {
        fputc(ch, out);
        printf("%c", ch);

    }


    fclose(in);
    fclose(out);
    printf("\n");
    system("pause");
    return EXIT_SUCCESS;
}

二,一行一行读取文件fgets和fputs

文本格式读写的
函数原型

       int fgetc(FILE *stream);

       char *fgets(char *s, int size, FILE *stream);

实际例子:

/*************************************************************************
> File Name:
> Author: songli
> QQ:2734030745
> Mail: 15850774503@163.com
> Created Time:
************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, char *argv[])
{
    printf("请您输入文件的路径:\n");

    char filename1[50], filename2[50];
    //在堆上开辟内存1kb
    char *buf = (char*)malloc(sizeof(char) * 1024); 
    scanf("%s", filename1);
    FILE *in = fopen(filename1, "rb");
    if (!in)
        return -1;
    printf("请您输入文件的路径:\n");
    scanf("%s", filename2);
    FILE *out = fopen(filename2, "wb");
    if (!out)
        return -1;

    while (!feof(in))
    {
        fgets(buf, sizeof(char) * 1024, in);

        //写的文件中
        fputs(buf, out);
    }

    //释放内存
    free(buf);
    fclose(in);
    fclose(out);
    printf("\n");
    system("pause");
    return EXIT_SUCCESS;
}

三,格式化输入和输出fscanf和fprintf的函数

函数模型

       #include <stdio.h>

       int scanf(const char *format, ...);
       int fscanf(FILE *stream, const char *format, ...);
       int sscanf(const char *str, const char *format, ...);
/*************************************************************************
> File Name:
> Author: songli
> QQ:2734030745
> Mail: 15850774503@163.com
> Created Time:
************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, char *argv[])
{
    printf("请您输入文件的路径:\n");

    char filename1[50], filename2[50];

    scanf("%s", filename1);
    FILE *in = fopen(filename1, "rb");
    if (!in)
        return -1;
    printf("请您输入文件的路径:\n");
    scanf("%s", filename2);
    FILE *out = fopen(filename2, "wb");
    if (!out)
        return -1;
    int a, b;
    char c;
    while (!feof(in))
    {
        //这边我就简单写一下   可以用开辟堆上的内存的指针操作
        fscanf(in, "%d%c%d=\n", &a, &c, &b);

        printf("%d,%c%d\n", a, c, b);

        //写入一个文件中

        fprintf(out, "%d%c%d=\n", a, c, b);

    }


    fclose(in);
    fclose(out);
    printf("\n");
    system("pause");
    return EXIT_SUCCESS;
}

效果图
这里写图片描述

四,文件随机读写好的函数fseek和ftell的使用

函数


       int fseek(FILE *stream, long offset, int whence); //SEEK_SET, SEEK_CUR, SEEK_END
       long ftell(FILE *stream);

       void rewind(FILE *stream);

       int fgetpos(FILE *stream, fpos_t *pos);
       int fsetpos(FILE *stream, const fpos_t *pos);

五,文件的拷贝和切割fread和fwrite

函数的定义

#include <stdio.h>

       size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

       size_t fwrite(const void *ptr, size_t size, size_t nmemb,
                     FILE *stream);

例子:

/*************************************************************************
> File Name:
> Author: songli
> QQ:2734030745
> Mail: 15850774503@163.com
> Created Time:
************************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>



int main(int argc, char *argv[])
{
    printf("请您输入文件的路径:\n");

    char filename1[50], filename2[50];
    //在堆上开辟内存1kb
    char *buf = (char*)malloc(sizeof(char) * 1024);
    scanf("%s", filename1);
    FILE *in = fopen(filename1, "rb");
    if (!in)
        return -1;
    printf("请您输入文件的路径:\n");
    scanf("%s", filename2);
    FILE *out = fopen(filename2, "wb");
    if (!out)
        return -1;

    while (!feof(in))
    {
        int len = fread(buf, sizeof(char), 1024, in);

        fwrite(buf, sizeof(char), len, out);
    }

    //释放内存
    free(buf);
    fclose(in);
    fclose(out);
    printf("\n");
    system("pause");
    return EXIT_SUCCESS;
}

效果图
这里写图片描述

这里我要讲的fread函数的
的参数的返回值是读取的到数据的大小

int len = fread(buf, sizeof(char), 1024, in);

六,文件的信息的函数stat

 #include <sys/types.h>
       #include <sys/stat.h>
       #include <unistd.h>

       int stat(const char *pathname, struct stat *buf);
       int fstat(int fd, struct stat *buf);
       int lstat(const char *pathname, struct stat *buf);

       #include <fcntl.h>           /* Definition of AT_* constants */


 struct stat {
               dev_t     st_dev;         /* ID of device contai
ning file */
               ino_t     st_ino;         /* inode number */
               mode_t    st_mode;        /* protection */
               nlink_t   st_nlink;       /* number of hard links */
               uid_t     st_uid;         /* user ID of owner */
               gid_t     st_gid;         /* group ID of owner */
               dev_t     st_rdev;        /* device ID (if special file) */
               off_t     st_size;        /* total size, in bytes */
               blksize_t st_blksize;     /* blocksize for filesystem I/O */
               blkcnt_t  st_blocks;      /* number of 512B blocks allocated */

               /* Since Linux 2.6, the kernel supports nanosecond
                  precision for the following timestamp fields.
                  For the details before Linux 2.6, see NOTES. */

               struct timespec st_atim;  /* time of last access */
               struct timespec st_mtim;  /* time of last modification */
               struct timespec st_ctim;  /* time of last status change */

           #define st_atime st_atim.tv_sec      /* Backward compatibility */
           #define st_mtime st_mtim.tv_sec
           #define st_ctime st_ctim.tv_sec
           };

还有几个还是没有讲到 分别是重置文件光标位置:

rewind(文件指针)

文件属性:
1、导入头文件 sys/types.h sys/stat.h
2、定义文件状态结构体 struct stat st
3、获取文件状态 stat(文件名,&st)
4、获取文件大小st.st_size

删除:
remove(路径+文件名)成功返回值为0

重命名(移动):
rename(路径+老文件名,路径+新文件名)成功返回值为0

更新文件缓冲区:
fflush(文件指针)

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值