【问题标题】:check if file is empty or not in c [duplicate]在c中检查文件是否为空[重复]
【发布时间】:2015-07-19 22:09:39
【问题描述】:

我想检查我的文本文件是否包含数据。如果文件包含我要读取的数据。我的问题是我不知道在 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 );
    }
   }

【问题讨论】:

标签: c


【解决方案1】:

它不起作用,因为你必须先 fopen 并 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 );
}

【讨论】:

  • 它可以很好地知道文件是否为空,但它会影响从文件中读取。它使 fscanf 读取像垃圾一样的东西。当我删除这些代码行时,它会从文件中很好地读取: fseek(fptr, 0, SEEK_END);无符号长 len = (无符号长)ftell(fptr); if (len > 0) { //检查文件是否为空。请帮帮我。
  • 您可能想使用 rewind(fptr) 回到开头;在阅读之前,是的
  • 不幸的是。还不行。
  • 请写下完整的正确代码。
  • 我测试了代码,它适用于我只包含浮点数的文件。因此,请发布您的文件内容。
【解决方案2】:

你可以使用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;
}

或者您可以使用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;
}

【讨论】:

    【解决方案3】:

    调用ftell() 不会告诉你文件的大小,它会告诉你文件位置指示器的当前值,当你第一次打开文件时,它总是0。

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

    所以基本上,

    #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 );
    }
    

    }

    希望这会有所帮助。

    【讨论】:

    • 文件 *fptr; if ( !( fptr = fopen( "saving.txt", "r" ))){ printf( "无法打开文件以从中检索您的数据。\n" ); } else { fseek(fptr, 0, SEEK_END);无符号长 len = (无符号长)ftell(fptr); if (len > 0) { //检查文件是否为空。倒带(fptr); while ( !feof( fptr ) ){ fscanf( fptr, "%f\n", &p.burst_time ); AddProcess(&l,p.burst_time); } } fclose( fptr );它告诉我文件是否为空,但它会影响从文件中读取。它使 fscanf 读取像垃圾一样的东西。
    猜你喜欢
    • 2011-05-03
    • 2014-12-16
    • 1970-01-01
    • 2013-11-30
    • 2013-05-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    相关资源
    最近更新 更多