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

标签 c pipeline

请原谅我的无知,我是 C 的新手。作为一种学习经验,我正在尝试读取一张图像,该图像已被 cat 编辑并通过管道传输到一个简单的 C 脚本。我想遍历八位字节,至少现在,只需将它们打印到 STDOUT。

示例:cat some-image.png | ./我的-c-程序

代码:

#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,您会得到段错误。

要使用标准 C I/O 函数从标准输入读取,请使用一个或多个

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

您不需要打开 stdin,因为 C 运行时启动代码会为您完成。如果您需要从参数提供的文件中读取,您需要先使用 fopen() 来创建一个新的输入流。

关于c - 如何从 C 中的 STDIN 或 Unix 管道读取流,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29021540/

相关文章:

C:关于 rand、random 和 arc4random 的最终真相

c - 用随机字母填充二维数组-C编程

bash 脚本 : how to save return value of first command in a pipeline?

python - 我应该直接返回数据集还是应该使用 one_shot 迭代器?

python - 如何在流水线后对回归预测进行逆变换?

c - 为什么显示的地址为负值

c - 为什么可以将字符串指针初始化为字符串文字,而不是数组?

c - Ce 中的迷宫回溯

c# - .net 核心用于 ubuntu linux 流水线

python - 属性错误 : 'Pipeline' object has no attribute '_transfer_param_map_to_java'