C | File operation (two)

1. Copy of large files

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#define SIZE 1024  //注意没有分号
int main() {
    FILE* fp = fopen("./cn.wav", "rb");
    if (!fp)
    {
        return -1;
    }
    FILE* fp1 = fopen("./copy.wav", "wb");
    char* temp = (char*)malloc(sizeof(char) * SIZE);
    int real_count = 0;//主要记录最后一包读到的真正块数,确保拷贝的新文件大小等于原文件大小
    while (!feof(fp))
    {
        memset(temp, 0, SIZE);
        real_count= fread(temp, sizeof(char), SIZE, fp);
        printf("%d\n", real_count);
        fwrite(temp, sizeof(char), real_count, fp1);
    }
    free(temp);
    fclose(fp);
    fclose(fp1);
    return 0;
}

 Second, get the status of the file

1. Function call

struct stat
{
    dev_t st_dev; //device 文件的设备编号
    ino_t st_ino; //inode 文件的索引节点
    mode_t st_mode; //protection 文件的类型和存取的权限
    nlink_t st_nlink; //number of hard links 连到该文件的硬连接数目, 刚建立的文件值为1.
    uid_t st_uid; //user ID of owner 文件所有者的用户识别码
    gid_t st_gid; //group ID of owner 文件所有者的组识别码
    dev_t st_rdev; //device type 若此文件为装置设备文件, 则为其设备编号
    off_t st_size; //total size, in bytes 文件大小, 以字节计算
    unsigned long st_blksize; //blocksize for filesystem I/O 文件系统的I/O 缓冲区大小.
    unsigned long st_blocks; //number of blocks allocated 占用文件区块的个数, 每一区块大小为512 个字节.
    time_t st_atime; //time of lastaccess 文件最近一次被存取或被执行的时间, 一般只有在用mknod、utime、read、write 与tructate 时改变.
    time_t st_mtime; //time of last modification 文件最后一次被修改的时间, 一般只有在用mknod、utime 和write 时才会改变
    time_t st_ctime; //time of last change i-node 最近一次被更改的时间, 此参数会在文件所有者、组、权限被更改时更新
};

#include <sys/types.h>

#include <sys/stat.h>

int stat(const char* path,struct stat* buf);

Parameters: path: file name (including file path)

          buf: a structure to save file information

Return value: success 0; failure -1;

2. Function usage:

#define _CRT_SECURE_NO_WARNINGS
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>

int main()
{
    struct stat _stat;
    int ret = stat("./copy.wav", &_stat);
    printf("%d\n", _stat.st_size);//107598
}

 

 

 

 

 

 

 

Guess you like

Origin blog.csdn.net/weixin_39766005/article/details/109653667