I want to find out the file size with C. Help me

Asked 2 years ago, Updated 2 years ago, 92 views

I want to know how to find out the size of the file. Before saving the entire file as a string, I want to read it after malloc just the size of the file.

I'm using malloc(MAXSIZE*sizeof(char)) together, but it's too inefficient

file c size

2022-09-22 22:20

1 Answers

ftell finds the distance between the start location of the file and the current fine pointer location

fseek(f, 0, SEEK_END); // Move the file pointer to the end of the file
size = ftell(f); // return the distance from the beginning to the end of the file
flook(f, 0, SEEK_SET); // Move the file pointer to the beginning of the file

stat returns information about the file in the structure.

#include <sys/stat.h>
struct stat st;
stat(filename, &st);
size = st.st_size;


2022-09-22 22:20

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.