如何从C中读取STDIN或Unix管道中的流

时间:2015-03-12 22:32:21

标签: c pipeline

原谅我的无知,我是C的新手。作为一种学习经历,我试图阅读的图像是cat&#39}用简单的C脚本传送。我想迭代八位字节,至少现在,只需将它们打印到STDOUT。

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

代码:

#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;
}

目前,我收到了很多SegFault 11

我不太清楚如何理解这一点,但Valgrind告诉我:

==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

感谢任何帮助或指导。

1 个答案:

答案 0 :(得分:2)

如果您没有向./my-c-program提供论据,那么argv[1]NULL,您就会遇到段错误。

要使用标准C I / O功能从标准输入读取,请使用

中的一个或多个
getchar(), fgetc(stdin), scanf(), fread(..., stdin), fgets(..., stdin)

您不需要打开stdin,因为C运行时启动代码会为您执行此操作。如果需要读取参数提供的文件,则需要先使用fopen()创建新的输入流。