繁体   English   中英

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

[英]How to read stream from STDIN or Unix pipe in C

原谅我的无知,我是全新的,以C.作为一个学习的经验,我想这一直是图像中读取cat “ED和管道输送到一个简单的C脚本。 我想遍历这些八位位组,至少到目前为止,只需将它们打印到STDOUT。

示例: cat some-image.png | ./my-c-program 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

任何帮助或指导表示赞赏。

如果不提供./my-c-program的参数,则argv[1]NULL并且会出现段错误。

要使用标准CI / O功能从标准输入中读取信息,请使用以下一项或多项内容:

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM