Checking for an empty file in C++

前端 未结 8 2010
抹茶落季
抹茶落季 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:59

    pFile = fopen("file", "r");
    fseek (pFile, 0, SEEK_END);
    size=ftell (pFile);
    if (size) {
      fseek(pFile, 0, SEEK_SET);
      do something...
    }
    
    fclose(pFile)
    

提交回复
热议问题