size of file in c

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

showing results for - "size of file in c"
Yassin
28 Mar 2016
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;
Shanna
27 Jul 2018
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;
Elisa
09 Jul 2019
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
how to get file name and its type using metadata in linux using cget length of file csize of the file in cc small file sizehow to find the length of a file in chow to get a size of the file linux cc file sizeget file size using fseekget size of file cget size of file c openfread 2c size of file in csize of file fopenhow to get the size of any file in cc open file sizelinux get file size fseekfind file size cdimension char to size of filesize of file in cusing ftell as array size cget fiel size stdioget size of file in cc length of fileget file size using statcalculate file length csize of file cwhat is the size of file in ccheck size of file linuxc get length of filehow to create a file in c with a sizefile size in c statknowbytes of a file chow to get file content size in cget allocated size of file chwo to get the size of a file in chow to see in a file wich is bigger in clinux file size c programwhat is the size of data type file in cget length of file in ccalculate file size in cfind the length of a file in chow to calculate size of file in cc get file size openknow bytes of a file cc program to get file sizehow to know size of file in clinux check size of a filec get file sizec size of filehow to find size of a file in cthe size of files on cfind total size of a file in cget file size csize of a file in cc get file size unixc decrease file sizefind the size of the file in cget file size c sizeoffile length in cc find length of a filec sizeof filelength of a file in clinux read file size chow to find the size of a file c 2b 2b linuxhow can i see the size of a file in chow to know swap file size in linuxfile size in cwrite c program to calculate the length of a filefind big files size in cc how to get size of file in bytesfind big file size in csizeof file cc how to get the size of a fliecheck the size of the file in linuxhow to get the size of a file in chow to check the size of a binary file cc show amount of bytes in a filefind file length cc how to get size of filec code size of fileget file lengthhow to get a size of a file in csizeof file type cc determine file sizehow to find file size in cget the size of a file chow to find the size of file in read function in cget file stream filename and size csize of file from fseekhow to get length of file in cfilesize em cfastest way to get file size in cidentify file size in chow to know size of file in linuxhow to check the size of a file cfind size of a text file in cc programming check file sizehow to make c know the size of a filefscanf c size of filegettinf a file size in cmodify size of file in cc mesure file sizec file lengthc get the size of a file in charhow to find the size of a file in cc print current file sizec length of a filesize of a file cmake a file smaller in size in cget file size opening a fileget sizeof file in chow free in c know the sizeget size in bytes of file cfind size of text file chow to find the size of write file using fd in ccan you use sizeof command on files using cc check size of filefstat get file sizefile get size chow to get size of file in chow sizeof works in crealloc in c and size of a filefind size of binary file in csize write file in ccheck file size chow to get actual size of a file in cfile length cc using stat to get file sizeprint file size in chow to check the size of a file in linuxdetermine length of file in cdisplay size of text file c programmingsizeof file in clenght of file in csizeof c filec get size of filelinux find out size of filec get the total bytes from filelength of file cget file size of file in windows cfile size chow to check size of file in linuxhow to get file size in cbinary file size c fileswitch for ls to view innode number linuxhow to get the file size in chwo big should your main file be in cfind size of a file in cc get file size in bytesget file size of file linux chow to check the size of a file in linux cget size of a file in cget file size unix cget file size in cc measure file sizec size file 2ac read file sizehow to specify size of created file in chow to know the size of a file in linuxget size of a file chow to display the size of a file in chow to find the size of write file in read function in cf size 28 29 example cc print file sizesize of file in c