文件操作(stat函数)
生活随笔
收集整理的這篇文章主要介紹了
文件操作(stat函数)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
stat函數可以獲取文件信息
/*** stat.c ***/ #include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h>int main() {struct stat st = {0}; //定義一個結構體,名字叫ststat("./a.txt",&st); //調用完stat函數之后,文件的相關信息就保存再st結構中char *array = malloc(st.st_size); //st.st_size 表示文件的大小,在堆中動態分配一塊文件大小的內存FILE *p = fopen("./a.txt","rb");fread(array,sizeof(char),st.st_size,p); //相當于把整個文件一下子放入內存中 fclose(p);p = fopen("./b.txt","wb");fwrite(array,sizeof(char),st.st_size,p); //將堆中的信息一下寫入文件中 fclose(p); }可以使用時間相關函數來計算一下程序運行的時間(對于大文件復制有很好的效果)
#include<stdio.h> #include<string.h> #include<sys/stat.h> #include<stdlib.h> #include<time.h>int main() {clock_t c1 = clock();struct stat st = {0};stat("./a.txt",&st);char *array = malloc(st.st_size);FILE *p = fopen("./a.txt","rb");fread(array,sizeof(char),st.st_size,p);fclose(p);p = fopen("./b.txt","wb");fwrite(array,sizeof(char),st.st_size,p);fclose(p);clock_t c2 = clock();printf("%u\n",c2-c1); }?
轉載于:https://www.cnblogs.com/wanghao-boke/p/11240344.html
總結
以上是生活随笔為你收集整理的文件操作(stat函数)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 成都欢乐谷幼儿能玩吗
- 下一篇: 为什么lol那些女的,不会玩辅助还能躺着