c - 我遇到了一个非常奇怪的计时错误,有些东西在我认为应该执行之前就已经执行了

标签 c file printing io

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

int c, n, E, b, s, v, t, opt, valid = 0;
char current = '\0';
char previous = '\0';
FILE *fp;

/*  -n numbers lines
 *  -E appends a dollar sign to line ends
 *  -b numbers only non-blank lines
 *  -s squeezes multiple blank lines down to 1
 *  -v displays control chars, excluding tab
 *  -t includes tab in the above
 *  -e is the same as -E and -v
 */

int setFlags(int argc, char *argv[]) {
    int op;
    while ((op = getopt(argc, argv, "nEbsvte")) != -1) {
        switch (op) {
            case 'n': {
                n = 1;
                break;
            } case 'E': {
                E = 1;
                break;
            } case 'b': {
                b = 1;
                break;
            } case 's': {
                s = 1;
                break;
            } case 'v': {
                v = 1;
                break;
            } case 't': {
                t = 1;
                break;
            } case 'e': {
                E = 1;
                v = 1;
                break;
            } case '?': {
                //fprintf(stderr, "Option `-%c` is not valid.\n", optopt);
                return EXIT_FAILURE;
            } default: {
                abort();
            }
        }
    }
    opt = optind;

    if(n == 1) {
        b = 0;
    }

    return EXIT_SUCCESS;
}

int checkFile(char *path) {
    if (access(path, R_OK) == 0) {
        return EXIT_SUCCESS;
    } else {
        fprintf(stderr, "cat: %s: %s\n", argv[i], strerror(errno));
        errno = 0;
        return EXIT_FAILURE;
    }
}

int doPrint(char *path) {
    if (strcmp(path, "stdin") == 0) {
        fp = stdin;
    } else {
        if (checkFile(path) == 1) {
            return EXIT_FAILURE;
        } else {
            fp = fopen(path, "r");
        }
    }
    while ((c = fgetc(fp)) != EOF) {
        putchar(c);
    }
    fclose(fp);
    return EXIT_SUCCESS;
}

int main (int argc, char *argv[]) {
    if (setFlags(argc, argv) == 1) {
        fprintf(stderr, "The program has terminated with an error.\n"
        "An invalid option was specified.\n");
        return EXIT_FAILURE;
    } else {
        if ((argc - opt) == 0) {
            doPrint("stdin");
        } else {
            for(int i = opt; i < argc; i++) {
                doPrint(argv[i]);
            }
        }
    }
}

我遇到了一个非常疯狂的错误,我的程序在完成写入文件内容之前在 checkFile 中输出错误行(总是在结束前进行一次聊天)。

这让我发疯,无论我将这段代码移至何处,它都无法按预期工作。

我确信答案可能是微不足道的,但它让我难住了。我什至在输出完成之前陷休眠眠和各种其他事情,它会抛出错误,然后 sleep ,然后打印最终字符。

有什么帮助吗?

最佳答案

使用printf时,默认情况下会缓冲stdout输出。这意味着它可以与其他输出交错,通常来自 stderrstderr 默认情况下是无缓冲的,因此当发生错误时,它的输出会立即打印,正如通常所希望的那样。

可以通过明智地使用fflush或使用setbuf关闭stdout的文件缓冲来修复交错。请务必阅读 setbuf 的手册页,因为有一些注意事项。

在这种情况下,在 doPrint 函数末尾添加 fflush(stdout) 应该可以解决“问题”。

关于c - 我遇到了一个非常奇怪的计时错误,有些东西在我认为应该执行之前就已经执行了,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15354015/

相关文章:

perl - 更新终端上的多行 perl 输出

c - 如何避免 C 头文件覆盖 native C++ 类型

c++ - C 和 C++ 中静态变量存储在哪里?

java - 文件搜索时出现空指针异常

C#:导出到文本文件时,项目符号乱七八糟

powershell - 为什么 powershell(ise) 有时会打印出我执行的代码?

c++ - 用于创建多边形的 C/C++ PNG 库

有人可以解释错误吗?

php - 使用 htaccess 使 PHP 文件上传安全

java - 将数据从 java 传递到 cups 过滤器