简体   繁体   English

如何检查C中的文件是否为空?

[英]How do i check if a file is empty in C?

I'm importing a txtfile into my file, how do i check if the input file is blank. 我正在将txtfile导入文件,如何检查输入文件是否为空白。

I already check if it cannot read the input. 我已经检查了它是否无法读取输入。 This is what i have so far: 这是我到目前为止所拥有的:

#include<stdio.h>
#include<stdlib.h>

int main (int argc, char *argv[]){

// argv[1] will contain the file name input. 

FILE *file = fopen(argv[1], "r");

// need to make sure the file is not empty, error case. 

if (file == NULL){
    printf("error");
    exit(0);
}
 // if the file is empty, print an empty line.

int size = ftell(file); // see if file is empty (size 0)
if (size == 0){
    printf("\n");
}
printf("%d",size);

the size checking obviously does not work because i put in a few numbers and the size is still 0. any suggestions? 大小检查显然不起作用,因为我输入了几个数字,大小仍然为0。有什么建议吗?

You can use sys/stat.h and call the value of the st_size struct 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;
}

how about try read first row. 怎么样阅读第一行。 and see what characters you get? 看看你得到什么字符?

Call ftell() does not tell you the size of the file. 调用ftell()不会告诉您文件的大小。 From the man page: 从手册页:

The ftell() function obtains the current value of the file position indicator for the stream pointed to by stream. ftell()函数获取stream指向的流的文件位置指示符的当前值。

That is, it tell you your current position in the file...which will always be 0 for a newly opened file. 也就是说,它告诉您文件中的当前位置...对于新打开的文件,该位置始终为0 You need to seek to the end of the file first (see fseek() ). 您需要先seek到文件的末尾(请参阅fseek() )。

ftell will tell you the position the file pointer is at, and immediately after you opened the file, this position is always 0. ftell会告诉您文件指针所处的位置 ,打开文件后,该位置始终为0。

You either use stat before opening, or use fseek to seek some distance in the file (or at end) and then use ftell . 您可以在打开前使用stat ,或使用fseek在文件中(或末尾)查找某个距离,然后使用ftell

Or you delay the check until afterwards. 或者您将检查延迟到之后。 Ie, you try to read whatever you need to read, and then verify whether you succeeded or not. 即,您尝试阅读需要阅读的所有内容,然后验证您是否成功。

Update : and speaking of checks, you have no guarantee that 更新 :说到支票,您不能保证

// argv[1] will contain the file name input. 

For that, you need to check out that argc is at least 2 (the first argument being the executable name). 为此,您需要检查argc至少为2(第一个参数是可执行文件名称)。 Otherwise your file name might be NULL . 否则,您的文件名可能为NULL fopen should simply return NULL , but in other scenarios you might find yourself looking at a core dump. fopen应该只返回NULL ,但是在其他情况下,您可能会发现自己正在查看核心转储。

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

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