File operations (stat function)

stat function to obtain file information

/ * ** 
stat.c 
** * / 
#include <stdio.h> 
#include < String .h> 
#include <SYS / defined in stat.h> 
#include <stdlib.h> int main () 
{ struct STAT ST = { 0 };         // definition of a structure, called st 
    stat ( " ./a.txt " , & st);         // after the calling stat function, information about the file is saved again st structure char * Array the malloc = (st.st_size);         // st.st_size represents the size of the file, the file size of a dynamically allocated heap memory of 
    the fILE * P = the fopen ( " ./a.txt " , "


    
    RB " ); 
    fread (Array, the sizeof ( char ), st.st_size, P);         // corresponds to the entire file at once placed in memory 
    fclose (P); 
    P = the fopen ( " ./b.txt " , " wb " ); 
    fwrite (Array, sizeof ( char ), st.st_size, the p-);         // information about the heap written to the file 
    fclose (the p-); 
}

You can use time correlation function to calculate the running time (for large file copy with very good results)

#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);
}

 

Guess you like

Origin www.cnblogs.com/wanghao-boke/p/11240344.html