C 程序:查找任何文件大小

原文:https://www.studytonight.com/c/programs/files-and-streams/program-to-find-size-of-file

我们将使用fseek()ftell()函数来查找文件的大小。还有其他方法可以找到文件大小,比如循环整个文件内容并找出大小,但是文件处理功能使它变得容易得多。

下面是一个查找文件大小的程序。

下面是 C 语言教程,讲解 C 语言中的文件处理→C 语言中的文件处理

#include<stdio.h>
#include<conio.h>

void main()
{
    FILE *fp;
    char ch;
    int size = 0;

    fp = fopen("MyFile.txt", "r");
    if (fp == NULL)
    {
        printf("\nFile unable to open...");
    }
    else
    {
        printf("\nFile opened...");
    }
    fseek(fp, 0, 2);    /* File pointer at the end of file */
    size = ftell(fp);   /* Take a position of file pointer in size variable */
    printf("The size of given file is: %d\n", size);
    fclose(fp);
}