RubeOnRails RubeOnRails - 3 months ago 24
C Question

How to read stream from STDIN or Unix pipe in C

Forgive my ignorance, I'm brand new to C. As a learning experience, I'm trying to read in an image that's been

cat
'ed and piped to a simple C script. I'd like to iterate over the octets, and at least for now, just print them to STDOUT.

Example:
cat some-image.png | ./my-c-program


Code:

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

int main(int argc, char *argv[]) {
FILE *input;

input = fopen(argv[1], "r");

int c;
long retvalue = 0;

while (EOF != (c = fgetc(input))) {
retvalue++;
printf("%d", c);
}

printf("length: %ld", retvalue);

return 0;
}


Currently, I'm getting a lot of
SegFault 11


I'm not too sure how to make sense of this, but Valgrind tells me:

==90834== Memcheck, a memory error detector
==90834== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==90834== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==90834== Command: ./a.out
==90834==
--90834-- ./a.out:
--90834-- dSYM directory is missing; consider using --dsymutil=yes
==90834== Syscall param open(filename) points to unaddressable byte(s)
==90834== at 0x1002FA012: open$NOCANCEL (in /usr/lib/system/libsystem_kernel.dylib)
==90834== by 0x1001EE4B6: fopen (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EDC: main (in ./a.out)
==90834== Address 0x0 is not stack'd, malloc'd or (recently) free'd
==90834==
==90834== Invalid read of size 8
==90834== at 0x1001ECE07: flockfile (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x1001ED52E: fgetc (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EF1: main (in ./a.out)
==90834== Address 0x68 is not stack'd, malloc'd or (recently) free'd
==90834==
==90834==
==90834== Process terminating with default action of signal 11 (SIGSEGV)
==90834== Access not within mapped region at address 0x68
==90834== at 0x1001ECE07: flockfile (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x1001ED52E: fgetc (in /usr/lib/system/libsystem_c.dylib)
==90834== by 0x100000EF1: main (in ./a.out)
==90834== If you believe this happened as a result of a stack
==90834== overflow in your program's main thread (unlikely but
==90834== possible), you can try to increase the size of the
==90834== main thread stack using the --main-stacksize= flag.
==90834== The main thread stack size used in this run was 8388608.
==90834==
==90834== HEAP SUMMARY:
==90834== in use at exit: 34,916 bytes in 425 blocks
==90834== total heap usage: 505 allocs, 80 frees, 41,044 bytes allocated
==90834==
==90834== LEAK SUMMARY:
==90834== definitely lost: 0 bytes in 0 blocks
==90834== indirectly lost: 0 bytes in 0 blocks
==90834== possibly lost: 0 bytes in 0 blocks
==90834== still reachable: 0 bytes in 0 blocks
==90834== suppressed: 34,916 bytes in 425 blocks
==90834==
==90834== For counts of detected and suppressed errors, rerun with: -v
==90834== ERROR SUMMARY: 2 errors from 2 contexts (suppressed: 0 from 0)
Segmentation fault: 11


Any help or guidance is appreciated.

Answer

If you don't provide an argument to ./my-c-program then argv[1] is NULL and you get a segfault.

To read from standard input using the Standard C I/O functions, use one or more of

getchar(), fgetc(stdin), scanf(), fread(..., stdin), fgets(..., stdin)

You do not need to open stdin as the C runtime startup code does that for you. If you need to read from files supplied by arguments, you need to use fopen() first to create a new input stream.

Comments