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 fscanf ignore commas 
C :: getchar c 
C :: c check if character is upper case 
C :: wifi access point in esp8266 
C :: text to hex in C 
C :: create arrya of chars malloc 
C :: c add char to char array 
C :: binary sorting 
C :: Compile multiple C files 
C :: c unused parameter 
C :: commenting in c 
C :: node in c 
C :: how to read and write to fiel n c 
C :: what is clrsce in C 
C :: localStorage.setItem multpile arra 
C :: recursion function bangla 
C :: what happens if i acess a freed variable in c 
C :: change base int in c 
C :: error: argument 1 range [18446744071562067968, 18446744073709551615] exceeds maximum object size 9223372036854775807 [-werror=alloc-size-larger-than=] 
C :: hgggggggggggggggg 
C :: 1 f = c 
C :: curl ftp upload file to directory 
C :: how many archaeologists are there 
C :: C Common mistakes when working with pointers 
C :: esp rainmaker led 
C :: wpdb add temporary while drop table 
C :: write varriable in file C 
C :: sdl close ev 
C :: C++ initalize int16_t value 
C :: hello world in c language 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =