C语言 在c中检查文件是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30133210/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:55:50  来源:igfitidea点击:

check if file is empty or not in c

c

提问by Hossam Atef El-Esseily

I want to check if my text file contains data or not. If the file contains a data I want to read it. My problem is I don't know the right condition to write in the if statement. Hint: I tried to use the functions of fseek & ftell, but without any benefit. I want to know why this condition in the if statement doesn't work properly?

我想检查我的文本文件是否包含数据。如果文件包含我想读取的数据。我的问题是我不知道在 if 语句中写入的正确条件。提示:我尝试使用 fseek & ftell 的功能,但没有任何好处。我想知道为什么 if 语句中的这个条件不能正常工作?

    FILE *fptr;
  if(ftell(fptr)!=0){   //check if the file is not empty.
    if ( !( fptr = fopen( "saving.txt", "r" ))){
        printf( "File could not be opened to retrieve your data from it.\n" );
    }
    else{
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
        fclose( fptr );
    }
   }

回答by h3n

it does'nt work because you have to fopen it and fseek to the end first:

它不起作用,因为你必须先打开它并 fseek 到最后:

FILE *fptr;
if ( !( fptr = fopen( "saving.txt", "r" ))){
    printf( "File could not be opened to retrieve your data from it.\n" );
}
else {
    fseek(fptr, 0, SEEK_END);
    unsigned long len = (unsigned long)ftell(fptr);
    if (len > 0) {  //check if the file is empty or not.
        rewind(fptr);
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
    }
    fclose( fptr );
}

回答by JGrindal

Call ftell()does not tell you the size of the file, it tells you the current value of the file position indicator which, when you first open the file, will always be 0.

Callftell()不会告诉您文件的大小,它会告诉您文件位置指示器的当前值,当您第一次打开文件时,该值始终为 0。

Using sys/stat.hand call the value of the st_sizemember, if its 0, your file is empty.

使用sys/stat.h并调用st_size成员的值,如果为 0,则您的文件为空。

So basically,

所以基本上,

#include <sys/stat.h>
char* filename; //I'm assuming this was defined somewhere earlier in your code
FILE *fptr;
struct stat fileStat;
stat(filename, &fileStat)

if( fileStat.st_size !=0 ){   //check if the file is not empty.
    if ( !( fptr = fopen( "saving.txt", "r" ))){
        printf( "File could not be opened to retrieve your data from it.\n" );
    }
    else{
        while ( !feof( fptr ) ){
            fscanf( fptr, "%f\n", &p.burst_time );
            AddProcess(&l,p.burst_time);
        }
    fclose( fptr );
}

}

}

Hope this helps.

希望这可以帮助。

回答by Ziezi

You can use fseekto the end, then check if ftellreturns 0.

您可以使用fseek到最后,然后检查是否ftell返回0

bool isEmpty(FILE *file){
    long savedOffset = ftell(file);
    fseek(file, 0, SEEK_END);

    if (ftell(file) == 0){
        return true;
    }

    fseek(file, savedOffset, SEEK_SET);
    return false;
}

or you can use sys/stat.hand call the value of the st_sizestruct member:

或者您可以使用sys/stat.h并调用st_size结构成员的值:

#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>

int main (int argc, char *argv[]) {
   if (argc != 2) {
   return EXIT_FAILURE;
   }

   const char *filename = argv[1];
   struct stat st;

   if (stat(filename, &st) != 0) {
       return EXIT_FAILURE;
   }
   fprintf(stdout, "file size: %zd\n", st.st_size);
   return EXIT_SUCCESS;
}