eat_a_lemon
eat_a_lemon

Reputation: 3208

C program when segfault stdout redirect not being captured in a text file

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

int main(int argc, char* argv[]) {
        printf("hello dude\n");
        //cause segfault
        *(int*)0 = 0;
        return 0;
}
gcc test.c -o test
./test &> out.txt
Segmentation fault (core dumped)
./test > out.txt
Segmentation fault (core dumped)
./test 1> out.txt
Segmentation fault (core dumped)

Open the text file and "hello dude" is never written. If you comment out the line that causes the segfault then "hello dude" gets written to the file. Some how the segfault is interrupting stdout. Is there something that can be on the command line to capture the output? I tried both cygwin bash and linux bash shells.

Upvotes: 1

Views: 1315

Answers (1)

eat_a_lemon
eat_a_lemon

Reputation: 3208

Adding

fflush(stdout)

after the printf works

Upvotes: 2

Related Questions