Checking for an empty file in C++

前端 未结 8 2017
抹茶落季
抹茶落季 2020-11-27 03:35

Is there an easy way to check if a file is empty. Like if you are passing a file to a function and you realize it\'s empty, then you close it right away? Thanks.

E

8条回答
  •  死守一世寂寞
    2020-11-27 03:47

    char ch;
    FILE *f = fopen("file.txt", "r");
    
    if(fscanf(f,"%c",&ch)==EOF)
    {
        printf("File is Empty");
    }
    fclose(f);
    

提交回复
热议问题