c get file size
FILE *fp = fopen("example.txt", "r");
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
c get file size
FILE *fp = fopen("example.txt", "r");
fseek(fp, 0L, SEEK_END);
int size = ftell(fp);
how to check the size of a file in linux c
If you have the file stream (FILE * f):
fseek(f, 0, SEEK_END); // seek to end of file
size = ftell(f); // get current file pointer
fseek(f, 0, SEEK_SET); // seek back to beginning of file
// proceed with allocating memory and reading the file
Or,
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
fd = fileno(f);
struct stat buf;
fstat(fd, &buf);
int size = buf.st_size;
Or, use stat, if you know the filename:
#include <sys/stat.h>
struct stat st;
stat(filename, &st);
size = st.st_size;
c file size
If you use :
/**
Pay attention to return type!
ftell returns long, which can only be used for files under 2GB.
**/
fseek(f, 0, SEEK_END); // seek to end of file
size = ftell(f); // get current file pointer
fseek(f, 0, SEEK_SET); // seek back to beginning of file
// If u want to get file of large size, use stat() instead
struct stat buf;
if(stat(filename, &buf) == -1){
return -1;
}
return (ssize_t)buf.st_size;
size of file in c
// C program to find the size of file
#include <stdio.h>
long int findSize(char file_name[])
{
// opening the file in read mode
FILE* fp = fopen(file_name, "r");
// checking if the file exist or not
if (fp == NULL) {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
// calculating the size of the file
long int res = ftell(fp);
// closing the file
fclose(fp);
return res;
}
// Driver code
int main()
{
char file_name[] = { "a.txt" };
long int res = findSize(file_name);
if (res != -1)
printf("Size of the file is %ld bytes \n", res);
return 0;
}
Copyright © 2021 Codeinu
Forgot your account's password or having trouble logging into your Account? Don't worry, we'll help you to get back your account. Enter your email address and we'll send you a recovery link to reset your password. If you are experiencing problems resetting your password contact us