简体   繁体   English

我怎么知道文本文件是否为空?(在C中)

[英]How can I know text file is empty or not?(in C)

I'm trying to detect text file is empty or not in C. (values are initialized in NULL) Whenever read first in value(using fscanf), it always returns file has zero, even if it has value "0" or "empty". 我试图在C中检测文本文件是否为空。(值以NULL初始化)每当读取值(使用fscanf)时,它总是返回文件为零,即使它具有值“0”或“空” ”。

How can I know the target text file is empty or not? 我怎么知道目标文本文件是否为空? (it should be distinguished even it has "0" in first letter) (即使它在第一个字母中有“0”也应该被区分)

If the file was successfully open for read, as per fopen(filename, "r") , you can verify if it is empty before any read operation this way: 如果文件已成功打开以进行读取,则按照fopen(filename, "r") ,您可以在执行任何读取操作之前验证它是否为空:

int is_empty_file(FILE *fp) {
    int c = getc(fp);
    if (c == EOF)
        return 1;
    ungetc(c, fp);
    return 0;
}

ungetc() is guaranteed to work for at least one character. ungetc()保证至少可以使用一个字符。 The above function will return 1 if the file is empty or if it cannot be read, due to an I/O error. 如果文件为空或由于I / O错误而无法读取,则上述函数将返回1 You can tell which by testing ferr(fp) or feof(fp) . 你可以通过测试ferr(fp)feof(fp)来判断哪一个。

If the file is a stream associated to a device or a terminal, the test code will block until at least one byte can be read, or end of file is signaled. 如果文件是与设备或终端相关联的流,则测试代码将阻塞,直到可以读取至少一个字节或发出文件结束信号。

If the file is a regular file, you could also use a system specific API to determine the file size, such as stat , lstat , fstat (on Posix systems). 如果文件是常规文件,您还可以使用特定于系统的API来确定文件大小,例如statlstatfstat (在Posix系统上)。

If you're under Linux, you can use stat or fstat : 如果你在Linux下,你可以使用statfstat

#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
int fstat(int fd, struct stat *buf);
int lstat(const char *path, struct stat *buf);

Will give you this information: 会给你这个信息:

struct stat {
    dev_t     st_dev;     /* ID of device containing file */
    ino_t     st_ino;     /* inode number */
    mode_t    st_mode;    /* protection */
    nlink_t   st_nlink;   /* number of hard links */
    uid_t     st_uid;     /* user ID of owner */
    gid_t     st_gid;     /* group ID of owner */
    dev_t     st_rdev;    /* device ID (if special file) */
    off_t     st_size;    /* total size, in bytes */
    blksize_t st_blksize; /* blocksize for file system I/O */
    blkcnt_t  st_blocks;  /* number of 512B blocks allocated */
    time_t    st_atime;   /* time of last access */
    time_t    st_mtime;   /* time of last modification */
    time_t    st_ctime;   /* time of last status change */
};

To check for the size using stat : 要使用stat检查大小:

struct stat info;

if(stat(filepath, &info) < 0) {
    /* error */
}

if(info.st_size == 0) { 
    /* file is empty */
}

Or using fstat : 或者使用fstat

int fd = open(filepath, O_RDONLY);
struct stat info;

if(fstat(fd, &info) < 0) {
    /* error */
}

if(info.st_size == 0) { 
    /* file is empty */
}

you can do using ftell 你可以用ftell

#include <stdio.h>

int get_file_size (char *filename) {
   FILE *fp;
   int len;

   fp = fopen(filename, "r");
   if( fp == NULL )  {
      perror ("Error opening file");
      return(-1);
   }
   fseek(fp, 0, SEEK_END);

   len = ftell(fp);
   fclose(fp);

   return(len);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM