Search
 
SCRIPT & CODE EXAMPLE
 

C

c get file size

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

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.
Comment

sizeof file c

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
Comment

PREVIOUS NEXT
Code Example
C :: c program hide console window 
C :: transpose of matrix using c program 
C :: octave dot operator 
C :: variably modified ‘_memory’ at file scope 
C :: C hello workld 
C :: express.static public 
C :: tainted love 
C :: get chunks of a mp4 in ffmpeg 
C :: yourkill071 
C :: reverse integer in c 
C :: successeur d’un entier donné 
C :: execute maven project in cmd 
C :: que es % en c 
C :: lldb set breakpoint function name 
C :: what is covert channel 
C :: random float number in C 
C :: pg_restore: error: input file appears to be a text format dump. Please use psql. 
C :: vbnet create and write on file 
C :: PATH_MAX 
C :: append to list in c 
C :: double array in c 
C :: c read n bytes code 
C :: c header file example 
C :: How to pass a struct value to a pthread in c? 
C :: bubble sort 
C :: set the nth bit 
C :: debian unhold packages 
C :: do while loop in c 
C :: create arrya of chars malloc 
C :: while loop c 
ADD CONTENT
Topic
Content
Source link
Name
2+2 =