English 中文(简体)
如何写回转头的产出,在C的休息室内存档?
原标题:How to write redirected output to file inside loop in C?

我正在撰写关于“不清”的C方案,该方案应将产出转至档案中,并每秒向档案中写一些案文:

#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(void) {
    int outDes = open("./output.txt", O_APPEND | O_WRONLY);
    dup2(outDes, 1);
    while(1) {
        printf("output text
");
        sleep(1);
    }
}

但它没有写到产出档案中。 我试图改变10个休息室的休息时间,我发现,这10条路段在丛书结束后一刻写到档案中。 这对我来说并不好,而我需要有一种无限的 lo。

当我不调整产出方向时,它都是ok,新线每秒出现在终点站。

我还试图把一人带走。

printf("text
");

在将产出转至档案之前。 之后,该方案实时将线上写到档案中,这是好的,但是也写到了第一个(没有改头)线。 我不想把第1行放在我的产出档案中,我不理解的是,在产出没有调整时,如何将其写成文件(也许自上下一期以来仍会转而去),以及它如何能造成这些线子在实时上突然书写。

谁能解释我如何工作?

最佳回答
  1. 您没有检查公开()和编(2)的回报价值。 如果公开()或编(2()失败,则在产出中只字不提。 txt。

    if (outDes < -1) {
        perror("open");
        return 1;
    }
    if (dup2(outDes, 1) == -1) {
        perror("dup2");
        return 1;
    }
    
  2. 斜流被缓冲,在真实档案说明中进行之前,这些文字在记忆中发生。

    在<代码>印本()之后添加一个<代码>fflush(stdout)。

问题回答

你们在很多不完美的C图书馆重新管理记录不佳的DWIMmy特征。 http://linux.die.net/man/3/isatty”rel=“nofollow”。 如果图书馆的定点(或许是假的)不变,它就会把国际不动产业联合会置于“在线缓冲”模式之中,这意味着它会把缓冲投入排在新线的书写之前,然后将其全部冲向本组织。 但是,如果记录员为而不是<>>>一个终点站,则该记录将国际交易日志置于“充分的缓冲”模式之下,在这种模式中,它把诸如<代码>BUFSIZ<>>>/代码”等产出推移之前的缓冲,并且不注意细线中断。

这通常是你想要的行为,但如果你想要的话(如本案一样),你可以将其改为setvbuf(3)。 这项职能(尽管我没有上述行为)是ISO标准C。

#include <stdio.h>
#include <unistd.h>

int
main(void)
{
    if (freopen("output.txt", "a", stdout)) {
        perror("freopen");
        return 1;
    }
    if (setvbuf(stdout, 0, _IOLBF, 0)) {
        perror("setvbuf");
        return 1;
    }

    for (;;) {
        puts("output text");
        sleep(1);
    }
    /* not reached */
}




相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...