/*** 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); }