首頁  >  文章  >  後端開發  >  C程式以查找檔案大小

C程式以查找檔案大小

PHPz
PHPz轉載
2023-08-25 18:17:07729瀏覽

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

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 &ldquo;b.txt&rdquo; 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程式以查找檔案大小的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除