更多資料請點擊:我的目錄
本篇僅用於記錄自己所學知識及應用,代碼仍可優化,僅供參考,如果發現有錯誤的地方,儘管留言於我,謝謝。
效果如下:
#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中
更多資料請點擊:我的目錄