Your problem stems from the fact that the Segmentation fault (core dumped) message is not generated by your program, it is generated by the shell in which you can the a.out command. The process looks approximately like this:
- Your program generates a segfault
- Your program receives a
SIGSEGV signal
- Your program exits
- The
wait() system call executed by your shell exits with a status code that indicates your program exited abnormally due to a SIGSEGV signal.
- The shell prints an error message
This is discussed in somewhat more detail in this answer.
If you want to capture this output, you can try something like:
$ sh -c 'trap "" 11; ./a.out' 1> output.txt 2> error.txt
$ cat error.txt
Segmentation fault (core dumped)
This will run your code in a subprocess and will inhibit the default handling of the segfault by the shell.
cat(e.g../a.out <input.txt >output.txt 2>error.txt