>  기사  >  백엔드 개발  >  파일 크기를 알아내는 C 프로그램

파일 크기를 알아내는 C 프로그램

PHPz
PHPz앞으로
2023-08-25 18:17:07675검색

파일 크기를 알아내는 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 &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으로 문의하시기 바랍니다. 삭제