How to get file size using stat() function in C

1 Answer

0 votes
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main()
{
    char file[100] = "d:\\data.txt";
	struct stat stats;

	printf("File: %s\n", file);
	
    if (stat(file, &stats) == 0) {
        printf("File size: %d bytes", (int)stats.st_size);

    }
    else {
        printf("Unable to get file size\n");
    }
    
    return 0;
}
        
          
          
          
/*
run:
          
File: d:\data.txt
File size: 40 bytes
       
*/

 



answered Jul 12, 2020 by avibootz

Related questions

1 answer 179 views
1 answer 208 views
1 answer 255 views
1 answer 679 views
1 answer 220 views
...