c get file size

Solutions on MaxInterview for c get file size by the best coders in the world

showing results for - "c get file size"
Eugénie
02 Nov 2020
1FILE *fp = fopen("example.txt", "r");
2fseek(fp, 0L, SEEK_END);
3int size = ftell(fp);
Tiffany
18 Aug 2017
1If you have the file stream (FILE * f):
2
3fseek(f, 0, SEEK_END); // seek to end of file
4size = ftell(f); // get current file pointer
5fseek(f, 0, SEEK_SET); // seek back to beginning of file
6// proceed with allocating memory and reading the file
7
8Or,
9
10#include <sys/types.h>
11#include <sys/stat.h>
12#include <unistd.h>
13fd = fileno(f); 
14struct stat buf;
15fstat(fd, &buf);
16int size = buf.st_size;
17
18Or, use stat, if you know the filename:
19
20#include <sys/stat.h>
21struct stat st;
22stat(filename, &st);
23size = st.st_size;
Eleonora
15 Aug 2020
1If you use :
2
3/** 
4	Pay attention to return type! 
5	ftell returns long, which can only be used for files under 2GB. 
6**/
7fseek(f, 0, SEEK_END); // seek to end of file
8size = ftell(f); // get current file pointer
9fseek(f, 0, SEEK_SET); // seek back to beginning of file
10
11// If u want to get file of large size, use stat() instead
12struct stat buf;
13if(stat(filename, &buf) == -1){
14   return -1;
15}
16return (ssize_t)buf.st_size;
Juan Diego
28 Mar 2017
1// C program to find the size of file 
2#include <stdio.h> 
3
4long int findSize(char file_name[]) 
5{ 
6	// opening the file in read mode 
7	FILE* fp = fopen(file_name, "r"); 
8
9	// checking if the file exist or not 
10	if (fp == NULL) { 
11		printf("File Not Found!\n"); 
12		return -1; 
13	} 
14
15	fseek(fp, 0L, SEEK_END); 
16
17	// calculating the size of the file 
18	long int res = ftell(fp); 
19
20	// closing the file 
21	fclose(fp); 
22
23	return res; 
24} 
25
26// Driver code 
27int main() 
28{ 
29	char file_name[] = { "a.txt" }; 
30	long int res = findSize(file_name); 
31	if (res != -1) 
32		printf("Size of the file is %ld bytes \n", res); 
33	return 0; 
34} 
35
queries leading to this page
get file lengthwhat is the size of file in chow to know a size of a file in cf size 28 29 example csize of file from fseekwrite c program to calculate the length of a filehwo to get the size of a file in csizeof file in cknowbytes of a file cfile length chow to calculate size of file in chwo big should your main file be in cfind the length of a file in cget fiel size stdioc using stat to get file sizehow to create a file in c with a sizehow to determine file size in chow to check the size of a file in linux cc code size of filefscanf c size of filefind size of binary file in chow to see in a file wich is bigger in ccalculate file length chow to check the size of a file cbinary file size c filesize of file cget allocated size of file cget length of file cc show amount of bytes in a filehow to display the size of a file in chow to get the file size in cc program to get file sizec get file lengthhow to specify size of created file in cdimension char to size of filec get file size lseek and readfstat get file sizehow to get length of file in cfind total size of a file in cfind file size clinux check size of a filec print file sizeget file size using fseekc mesure file sizehow to find the length of a file in cfile size in c statfread 2c size of file in cfastest way to get file size in chow to check the size of a binary file chow to find the size of write file using fd in chow to get file content size in cc how to get size of fileswitch for ls to view innode number linuxc get file size in bytesc how to get size of file in byteslinux find out size of filecheck size of file linuxget file size c posixfind big files size in cc get txt file sizeprint file size in clinux read file size cidentify file size in chow to find size of a file in cc decrease file sizehow to load a file in c sizec open file sizeget length of file in chow can i see the size of a file in cc get file size openhow sizeof works in chow to find the size of file in read function in cfind size of a file in csize of a file chow to get the size of any file in chow to get the size of a file in cc get the total bytes from filehow to get a size of a file in cc length of filec check size of filedisplay size of text file c programmingget file length chow to check size of file in linuxfile size cfile size in csizeof file type csize of file in cc determine file sizehow to know size of file in cc measure file sizec how to get the size of a flieget file size of file linux cmodify size of file in cfile get size chow to check the size of a file in linuxhow to know swap file size in linuxget size of file in cget the size of a file chow to get the size of a file cget sizeof file in cgettinf a file size in csize write file in chow to get file name and its type using metadata in linux using chow to now a size of a file in cc file sizehow to find the size of write file in read function in csizeof c fileget size in bytes of file chow to find the size of a file c 2b 2b linuxc get size of filec find length of a filemake a file smaller in size in cfile length in cc small file sizelinux file size c programc read whole file of any sizesize of a file in clength of a file in cc get the size of a file in charget file size in cget size of file cfind size of text file chow to find the size of a file in chow free in c know the sizeget file size opening a filegetting size of a file in cc get length of filehow to get size of file cknow bytes of a file cget file size cget file size using stathow to find the size of the file in cc size file 2ahow to know the size of a file in linuxdetermine length of file in clength of file ccheck the size of the file in linuxc size of filesizeof file cc read file sizec print current file sizelenght of file in ccheck file size cc length of a fileget size of a file crealloc in c and size of a fileusing ftell as array size csize of file fopenc get file size by descriptorget file size of file in windows cc get file size unixhow to get size of a file in cfind file length chow to find file size in chow to get file size in cc programming check file sizehow to get size of file in ccalculate file size in cget the size of a file in cthe size of files on cget size of a file in cc sizeof filefind big file size in cfind the size of the file in clinux get file size fseekhow to get a size of the file linux cfilesize em cc get file sizesize of the file in chow to make c know the size of a filecan you use sizeof command on files using cc file lengthget size of file c openfind size of a text file in chow to check size of file 2a in cget file stream filename and size cwhat is the size of data type file in cc program how to find file size of a fileget file size unix cget file size c sizeofhow to get actual size of a file in chow to know size of file in linuxhow to get file size cc get file size