文件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非零)
- 后面的
printf
和return
实际上不会执行