Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR C

How to get file size in C

// METHOD 1 (The quicker method):
FILE* file_ptr = fopen("myfile.txt", "r");
fseek(file_ptr, 0, SEEK_END);
long size = ftell(file_ptr);
// Don't forget to go back to the start of the file!
fseek(file_ptr, 0, SEEK_SET);
fclose(file_ptr);

// METHOD 2 (The better method):

#include <sys/stat.h> // This is included on your computer by default

char filename[] = "myfile.txt";
struct stat s;
int result = stat(filename, &s);
// stat() may return a -1 which means that there was an error.
if (result == -1) {
  puts("There was an error getting file info");
  exit(-1);
}
printf("The file is %f megabytes.
", sb.st_size / 1000000);
// I converted bytes to megabytes for easier-to-read results.
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #How #file #size #C
ADD COMMENT
Topic
Name
2+3 =