C程序来查找文件的大小

给定一个文本文件,以字节为单位查找其大小。

null

例如:

Input :  file_name = "a.txt"
         Let "a.txt" contains "geeks"
Output : 6 Bytes
There are 5 bytes for 5 characters then an extra
byte for end of file.

Input :  file_name = "a.txt"
         Let "a.txt" contains "geeks for geeks"
Output : 16 Bytes

这个想法是使用 C中的fseek() 弗特尔在C .使用fseek(),我们将文件指针移到末尾,然后使用ftell(),我们找到它的位置,它的大小实际上是字节。

// 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!" );
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 " , res);
return 0;
}


© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享