我怎么能确定一个文件是空的? [英] How can I determine if a file is empty?

查看:102
本文介绍了我怎么能确定一个文件是空的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何确定一个文件是空的?该文件由Windows平台上运行的C程序打开。我想打开追加模式的文件,而且,如果为空,请先打印头吧。

How do I determine if a file is empty? The file is opened by a C program running on the Windows platform. I want to open a file in append mode, and, if empty, first print a header to it.

// Open CSV & write header
report_csv = fopen("SNR.csv", "a+");
if (!report_csv) {
    fprintf(stderr, "Unable to open CSV output file...");
    return -1;
}
if (!ftell(report_csv)) {
    fprintf(report_csv, "Column A;Column B;Column C\n");
}
// ... print data to file
fclose(report_csv);

我期待 FTELL 返回当前文件的大小,如果该文件是不是空的,这是因为在code以上循环。

I was expecting ftell to return the current file size if the file was not empty, which happens because the code above is looped.

然而, FTELL 总是返回 0 和头被打印多次。

However, ftell always returns 0 and the header is printed multiple times.

我知道我可以的fopen 它 - [R ,并使用 fseek的 / FTELL / FCLOSE 然后的fopen 它再次与 A + ,但我认为这是可能做到这一点不开,多次关闭文件。

I know I could fopen it with r and use fseek/ftell/fclose and then fopen it again with a+, but I think it's possible to do this without opening and closing the file multiple times.

推荐答案

其实,当的fopen ING追加模式的文件,文件指针最初是在开始时文件。它只要你写的东西,或者使用移动到它的结束 fseek的

Actually, when fopening a file in append mode, the file pointer is initially at the begining of the file. It moves to the end of it as soon as you write something or use fseek.

我只需要添加 fseek的(report_csv,0,SEEK_END);(!FTELL(report_csv))在我的如果

让我们来看看这一点。结果
code 的:

#include <stdio.h>

int main(int argc, char **argv) {
    FILE *test;
    size_t size;
    char buf[100];

    /* Truncate file */
    test = fopen("test", "w");
    if (!test) {
        fprintf(stderr, "Cannot open file `test`!\n");
        return 1;
    }

    /* Write something */
    fprintf(test, "Something. ");
    fclose(test);

    /* Open in append */
    test = fopen("test", "a+");
    if (!test) {
        fprintf(stderr, "Cannot open `test` in append mode!\n");
        return 1;
    }

    /* Try to get the file size */
    size = ftell(test);
    printf("File pointer is: %d\n", size);
    fseek(test, 0, SEEK_END);
    size = ftell(test);
    printf("After `fseek(test, 0, SEEK_END)`, the file pointer is: %d\n", size);

    /* Append */
    fprintf(test, "And that. ");
    fclose(test);

    /* Same without fseek */
    test = fopen("test", "a+");
    if (!test) {
        fprintf(stderr, "Cannot open `test` in append mode!\n");
        return 1;
    }
    fprintf(test, "Hello! ");
    size = ftell(test);
    printf("File size is now: %d\n", size);
    fclose(test);

    /* Try to read */
    test = fopen("test", "r");
    if (!test) {
        fprintf(stderr, "Unable to open `test` for reading!\n");
        return 1;
    }
    printf("File contents:\n\t");
    while (test && !feof(test)) {
        fgets(buf, sizeof(buf), test);
        printf("%s", buf);
    }

    /* Cleanup & exit */
    fclose(test);
    printf("\n\nExiting.\n");

    return 0;
}

输出的:

File pointer is: 0
After `fseek(test, 0, SEEK_END)`, the file pointer is: 11
File size is now: 28
File contents:
        Something. And that. Hello!

Exiting.

这篇关于我怎么能确定一个文件是空的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆