C/C++获取文件大小

获取文件大小

概述

获取文件大小的方法有两种,一种是利用文件头和文件尾的偏移字节数来得到文件大小,另一种是利用文件状态信息中记录的文件总字节数来获取文件大小。利用 fseek()ftell() 函数可以计算出一个文件的大小。fseek() 函数用于设置文件指针 stream 的位置,ftell() 函数用于得到文件位置指针当前位置相对于文件首的偏移字节数。利用 stat() 函数可以获取文件的状态信息,包括文件大小,文件类型等。

使用偏移量获取文件大小

语法

#include <stdio.h>

int fseek(FILE *stream, long offset, int whence);
long ftell(FILE *stream);

函数说明

  • stream – 指向 FILE 对象的指针
  • offset – 相对 whence 的偏移量,以字节为单位(正数或负数)
  • whence – 设定从文件的哪里开始偏移。取值为:SEEK_SET(文件开头)、SEEK_CUR(当前位置)、SEEK_END(文件结尾)。

返回值

  • 如果成功,则该函数返回零,否则返回非零值。

示例

#include <stdio.h>

int main(int argc, char const *argv[]) {
    long size = 0;
    FILE *fp = fopen("/usr/bin/ls", "r");
    fseek(fp, 0, SEEK_END);
    size = ftell(fp);
    printf("size: %ld\n", size);
    /* fseek(fp, 0, SEEK_SET); */
    fclose(fp);
    return 0;
}
  • 第 5 行以只读形式打开 /usr/bin/ls 文件
  • 第 6 行调用 fseek() 函数并指向文件结尾
  • 第 7 行计算当前位置相对于文件首的偏移字节数,即得到文件大小值
  • 第 8 行打印文件大小
  • 第 9 行在该示例中注释掉了,当打开注释,指向文件开头,可以执行其他文件读写操作
  • 第 10 行使用完之后及时关闭文件指针

使用 stat 获取文件大小

stat() 函数获取的是文件的状态信息,不仅仅包含文件大小,还包含文件类型,用户 ID 等信息;该函数返回一个 struct stat 结构体,里面包含以下成员:

struct stat {
    dev_t     st_dev;         /* ID of device containing file */
    ino_t     st_ino;         /* Inode number */
    mode_t    st_mode;        /* File type and mode */
    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;     /* Block size 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
};

文件大小信息保存在 st_size 变量中,以字节为单位。

语法

#include <sys/stat.h>
int stat(const char *pathname, struct stat *statbuf);

函数说明

  • pathname – 路径字符串
  • statbuf – 文件状态信息结构体 struct stat 的对象指针

返回值

  • 如果成功返回 0;
  • 如果错误返回 -1,同时记录在 errno 中,可以通过 strerror() 函数获取错误码的字符串。

示例

#include <stdio.h>
#include <sys/stat.h>   // included for stat()

int main(int argc, char const *argv[]) {
    struct stat statbuf;
    stat("/usr/bin/ls", &statbuf);
    printf("size: %ld\n", statbuf.st_size);
    return 0;
}
  • 第 5 行定义文件状态结构体变量 statbuf
  • 第 6 行调用 stat() 函数获取文件状态,结果存储在 statbuf 变量中
  • 第 7 行打印实际的文件大小值 statbuf.st_size

欢迎关注我的公众号:飞翔的小黄鸭
也许会发现不一样的风景


▽ \bigtriangledown C/C++读写二进制文件

  • 2
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
获取网络实际使用率,可以使用系统提供的网络统计信息。在 C/C++ 中,可以通过调用系统 API 来获取这些信息。 在 Windows 操作系统中,可以使用 GetIfTable 和 GetIfEntry 函数来获取网络接口的流量统计信息。可以使用这些函数计算网络实际使用率。 在 Linux 操作系统中,可以使用 /proc/net/dev 文件来获取网络接口的流量统计信息。可以读取该文件并计算网络实际使用率。 以下是一个简单的 C++ 代码片段,用于获取 Windows 操作系统中网络接口的流量统计信息并计算网络实际使用率: ```cpp #include <Windows.h> #include <iphlpapi.h> #include <iostream> #pragma comment(lib, "iphlpapi.lib") int main() { MIB_IFTABLE* ifTable = nullptr; DWORD ifTableSize = 0; // 获取网络接口表格大小 GetIfTable(ifTable, &ifTableSize, false); // 分配缓冲区,获取网络接口表格 ifTable = (MIB_IFTABLE*) new char[ifTableSize]; GetIfTable(ifTable, &ifTableSize, false); // 遍历网络接口,输出实际使用率 for (DWORD i = 0; i < ifTable->dwNumEntries; ++i) { auto& ifEntry = ifTable->table[i]; // 计算网络实际使用率 ULONGLONG bytesSent = ifEntry.dwOutOctets; ULONGLONG bytesReceived = ifEntry.dwInOctets; ULONGLONG bytesTotal = bytesSent + bytesReceived; double utilization = bytesTotal / (double)ifEntry.dwSpeed; std::cout << "Interface: " << ifEntry.wszName << std::endl; std::cout << "Utilization: " << utilization << std::endl; } delete[] ifTable; return 0; } ``` 注意,上述代码仅适用于 Windows 操作系统。在 Linux 操作系统中,需要使用相应的 API 或文件来获取网络统计信息。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值