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 :: convert string to int c 
C :: millis() 
C :: getchar in c 
C :: actionbar content color in android 
C :: how to take comma separated integer input in c 
C :: variable swap in c 
C :: stdio.h 
C :: apt-mark remove hold 
C :: pandoc set margins pdf 
C :: open with overwrite c 
C :: how to insert elements in and array and print it in c 
C :: arduino empty serial buffer 
C :: c programming exercises 
C :: 1000000000 
C :: nested while loop in c 
C :: commenting in c 
C :: pointer in c 
C :: bitwise operators 
C :: fungetc 
C :: sOY wapo ya lo c 
C :: como somar em C 
C :: anthracnose pronounce 
C :: c create array 
C :: can we update values of a map via traversing 
C :: synopsis of fork() 
C :: 25802 WARNING: lib not found: _pywrap_tensorflow_internal.pyd dependency of D:Devtoolsminicondalibsite-packages ensorflowpythonclient\_pywrap_tf_session.pyd 
C :: determination data type in c 
C :: unia c 
C :: passage on dowry 
C :: exponent calculator 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =