当前位置: 首页 > news >正文

文件IO day29

五:stat

stat + 文件名 : 获得文件属性(主要是大小)

int stat(const char *pathname, struct stat *statbuf);

参数:path: 文件的路径
buf: 属性存放空间的首地址

eg: File: 07fdopen.cSize: 439       	Blocks: 8          IO Block: 4096   regular file
Device: 803h/2051d	Inode: 1458630     Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/   linux)   Gid: ( 1000/   linux)
Access: 2025-07-04 14:29:19.171911170 +0800
Modify: 2025-07-04 14:29:19.163911227 +0800
Change: 2025-07-04 14:29:19.163911227 +0800Birth: -
           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 nanosecondprecision 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};

六:时间函数

#include <stdio.h>
#include <time.h>int main(int argc, char *argv[])//time
{time_t tm;time(&tm);    printf("%ld\n",tm);return 0;
}
int main(int argc, char *argv[])//ctime
{time_t tm;time(&tm);    printf("%s\n",ctime(&tm));return 0;
}
       The ctime(), gmtime() and localtime() functions all take an argument ofdata type time_t, which represents calendar time.  When interpreted  asan  absolute  time  value,  it represents the number of seconds elapsedsince the Epoch, 1970-01-01 00:00:00 +0000 (UTC).
int main(int argc, char *argv[])//localtime
{time_t tm;time(&tm);    struct tm  * tm_info = localtime(&tm);printf("%d-%d-%d %d:%d:%d\n",tm_info->tm_year+1900,tm_info->tm_mon+1,tm_info->tm_mday,tm_info->tm_hour,tm_info->tm_min,tm_info->tm_sec);return 0;
}struct tm {int tm_sec;    /* Seconds (0-60) */int tm_min;    /* Minutes (0-59) */int tm_hour;   /* Hours (0-23) */int tm_mday;   /* Day of the month (1-31) */int tm_mon;    /* Month (0-11) */int tm_year;   /* Year - 1900 */int tm_wday;   /* Day of the week (0-6, Sunday = 0) */int tm_yday;   /* Day in the year (0-365, 1 Jan = 0) */int tm_isdst;  /* Daylight saving time */};

七:getpwuid/getgrgid

#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>
int main(int argc, char *argv[])
{int uid = 1000;struct passwd* pw = getpwuid(uid);if(NULL == pw){return 1;}printf("%s %s %d %s\n",pw->pw_name,pw->pw_passwd, pw->pw_gid,pw->pw_dir);return 0;
}
#include <stdio.h>
#include <grp.h>
#include <sys/types.h>
//                 char **
int main(int argc, char *argv[]) {int gid = 1000;struct group *gr = getgrgid(gid);printf("%s  %s ", gr->gr_name, gr->gr_passwd);int i = 0;while (NULL != gr->gr_mem[i]) {printf("%d %s\n", i, gr->gr_mem[i++]);}return 0;
}

八:提示报错perror/strerror/error

       void perror(const char *s);

功能: 打印s字符串和errno对应的错误信息
参数: s 要打印在终端上的出错信息
返回值: 缺省

#include <stdio.h>int main(int argc, char *argv[])
{FILE* fp = fopen("aaa","r");if(NULL == fp){//printf("open error\n");perror("fopen error");return 1;}return 0;
}
char *strerror(int errnum);

功能: 打印errnum出错码对应的出错信息
参数: errnum:出错errno
返回值: 成功返回对应的错误信息
ruturn 1;

int main(int argc, char *argv[])
{int i =  0;for(i= 0  ;i<255;i++){printf("%d %s\n",i,strerror(i));}return 0;
}
eg:
0 Success
1 Operation not permitted
2 No such file or directory
3 No such process
4 Interrupted system call
5 Input/output error
6 No such device or address
7 Argument list too long
8 Exec format error
9 Bad file descriptor
10 No child processes
11 Resource temporarily unavailable
12 Cannot allocate memory
13 Permission denied
14 Bad address
15 Block device required
16 Device or resource busy
17 File exists
18 Invalid cross-device link
19 No such device
20 Not a directory
21 Is a directory
22 Invalid argument
23 Too many open files in system
24 Too many open files
25 Inappropriate ioctl for device
26 Text file busy
27 File too large
28 No space left on device
29 Illegal seek
30 Read-only file system
31 Too many links
32 Broken pipe
33 Numerical argument out of domain
34 Numerical result out of range
35 Resource deadlock avoided
36 File name too long
37 No locks available
38 Function not implemented
39 Directory not empty
40 Too many levels of symbolic links
41 Unknown error 41
42 No message of desired type
43 Identifier removed
44 Channel number out of range
45 Level 2 not synchronized
46 Level 3 halted
47 Level 3 reset
48 Link number out of range
49 Protocol driver not attached
50 No CSI structure available
51 Level 2 halted
52 Invalid exchange
53 Invalid request descriptor
54 Exchange full
55 No anode
56 Invalid request codeand so on-----
void error(int status, int errnum, const char *format, ...);

功能:
程序出错打印对应出错原因和用户输入字符串并退出
参数:
status:程序退出的状态
EXIT_FAILURE 1
EXIT_SUCCESS 0
errnum:错误码
format:
类似printf打印
返回值:
缺省
__FILE__ 表示是那个文件
__LINE__表示第几行
__func__表示在那个函数
__DATE__
__TIME__

#include <stdio.h>
#include <error.h>
#include <errno.h>  int main(int argc, char *argv[])
{FILE* fp = fopen("aaa","r");if(NULL == fp){error(1,errno,"filename:%s func:%s line:%d",__FILE__,__func__,__LINE__);printf("aaaaaaaaaaa\n");return 1;}return 0;
}
  • error() 是GNU扩展,不是标准C函数
  • 它会自动在错误信息后添加换行并退出程序(如果status非零)
  • 后面的 printfreturn 实际上不会执行
http://www.dtcms.com/a/268723.html

相关文章:

  • 代码幽灵5-终局:静默编译
  • Baklib: 用 “技术轻量化” 解决内容管理 “重需求”
  • Linux命令的命令历史
  • 大数据在UI前端的应用创新:基于情感计算的用户体验优化
  • 冠能高端系列真实口碑如何
  • TCP backlog工作机制
  • AI时代,传统票务系统该往哪里使劲?
  • 华为手机如何扫描到SLE设备
  • 如何备份vivo手机中的联系人?
  • “猫攻击”揭示推理模型脆弱性,凸显上下文工程的重要性
  • 存储器介绍
  • React16,17,18,19新特性更新对比
  • 面向智驾的车规级高精度RTK模块UM680A的引脚功能
  • Git在Pycharm中的使用
  • web网页开发,在线%ctf管理%系统,基于html,css,webform,asp.net mvc, sqlserver, mysql
  • 【论文阅读】SASLN:小样本条件下机械故障诊断的信号增强自学习网络
  • Redis常用数据结构以及多并发场景下的使用分析:Set类型
  • react状态管理库 - zustand
  • BitMart“滑点守护计划”二期重磅升级,定义安心交易新纪元
  • Redis哨兵模式之Sentinel模式(二)
  • vue3 强制刷新 forceUpdate
  • 关于使用shiro中Session的使用导致的Java 对象引用问题
  • 【BTC】比特币系统的具体实现
  • 《30天打牢数模基础-第一版》(已完结) 需要自取
  • 浅析德语OCR技术的实现难点及其工作原理
  • 怎么删除音频空白部分_去掉mp3空白部分
  • FlashDepth | 混合模型+Mamba革新,24 FPS实时2K视频深度估计,超越Depth Anything v2
  • 生成ssh并配置到vscode和gitlab详细步骤
  • 什么是Web3?金融解决方案
  • 内网使用rustdesk搭建远程桌面详细版