FILE *fp = fopen("example.txt", "r");
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
// 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.
FILE *fp = fopen("file.txt", "w+");
fseek(fp,0,SEEK_END);
int size = ftell(fp) + 1; // we have to account for the 0th position in the file