C Get File Size

[Solved] C Get File Size | C - Code Explorer | yomemimo.com
Question : c get file size

Answered by : malo-c

FILE *fp = fopen("example.txt", "r");
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);

Source : https://stackoverflow.com/questions/238603/how-can-i-get-a-files-size-in-c | Last Update : Tue, 16 Mar 21

Question : How to get file size in C

Answered by : nolan-barker

// 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.\n", sb.st_size / 1000000);
// I converted bytes to megabytes for easier-to-read results.

Source : | Last Update : Fri, 26 Aug 22

Question : sizeof file c

Answered by : hi-there-tatj2layyi3n

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

Source : | Last Update : Tue, 03 May 22

Answers related to c get file size

Code Explorer Popular Question For C