算法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
example#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
以上就是c程序以查找文件大小的详细内容。