更多资料请点击:我的目录
本篇仅用于记录自己所学知识及应用,代码仍可优化,仅供参考,如果发现有错误的地方,尽管留言于我,谢谢。
效果如下:
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
int main(int argc, char **argv)
{
struct stat info; //结构体stat用来获取指定路径的文件或者文件夹的信息
bzero(&info , sizeof(info));
if(argc != 2) //判断输入参数是否正确
{
perror("输入格式错误!用法:<文件>\n");
exit(0);
}
FILE *src = fopen(argv[1], "w"); //标准IO获取指定文件的文件指针
if(src == NULL) //获取失败,返回NULL
{
perror("打开文件失败!\n");
exit(0);
}
while(1)
{
fwrite("a",1,1,src); //将若干块数据写入指定的文件
stat(argv[1], &info);
if(info.st_size > 0)
break;
}
printf("缓冲区大小: %ld\n",info.st_size);
fclose(src); //关闭指定的文件并释放其资源
return 0;
}
结构体 struct stat 详解
需要包含头文件:
#include <sys/stat.h>
#include <sys/types.h>
struct stat
{
dev_t st_dev; /* ID of device containing file -文件所在设备的ID*/
ino_t st_ino; /* inode number -inode节点号*/
mode_t st_mode; /* protection -保护模式*/
nlink_t st_nlink; /* number of hard links -链向此文件的连接数(硬连接)*/
uid_t st_uid; /* user ID of owner -user id*/
gid_t st_gid; /* group ID of owner - group id*/
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 blocks allocated -文件所占块数*/
time_t st_atime; /* time of last access -最近存取时间*/
time_t st_mtime; /* time of last modification -上次修改时间*/
time_t st_ctime; /* time of last status change -上次状态更改时间 */
};
正确——返回0
错误——返回-1,具体错误码保存在errno中
更多资料请点击:我的目录