C程序查找文件大小
这是一个C程序,用于查找文件的大小。
算法
Begin
function findfileSize() Open a file pointer fp in read only mode.
If fp is equals to null then
Print “File not found” and return -1.
Else count the file size.
Close the file.
Put the file pointer at the beginning of the file
Declare a integer variable result and initialize it with the output of the ftell() function.
Close file pointer fp.
Return result.
End示例
#include <stdio.h>
int findfileSize(char f_n[]) {
FILE* fp = fopen(f_n, "r"); // opening a file in read mode
if (fp == NULL) // checking whether the file exists or not {
printf("File Not Found!\n");
return -1;
}
fseek(fp, 0L, SEEK_END);
int res = ftell(fp); //counting the size of the file
fclose(fp); //closing the file
return res;
}
int main() {
char f_n[] = { "b.txt" }; //file name is “b.txt” whose size is to be determined
int result = findfileSize(f_n);
if (result != -1)
printf("Size of the file is %ld bytes \n", result); //printing the file size
return 0;
}输出结果
Size of the file is 2649 bytes
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语