如何从 C 中的 STDIN 或 Unix 管道读取流
How to read stream from STDIN or Unix pipe in C
请原谅我的无知,我是 C 的新手。作为一种学习经验,我正在尝试读入经过 cat
处理并通过管道传输到简单 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
感谢任何帮助或指导。
如果您不为 ./my-c-program
提供参数,则 argv[1]
为 NULL
,您会遇到段错误。
要使用标准 C I/O 函数从标准输入读取,请使用一个或多个
getchar(), fgetc(stdin), scanf(), fread(..., stdin), fgets(..., stdin)
您不需要打开 stdin
,因为 C 运行时启动代码会为您完成。如果您需要从参数提供的文件中读取,您需要先使用 fopen() 创建一个新的输入流。
请原谅我的无知,我是 C 的新手。作为一种学习经验,我正在尝试读入经过 cat
处理并通过管道传输到简单 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
感谢任何帮助或指导。
如果您不为 ./my-c-program
提供参数,则 argv[1]
为 NULL
,您会遇到段错误。
要使用标准 C I/O 函数从标准输入读取,请使用一个或多个
getchar(), fgetc(stdin), scanf(), fread(..., stdin), fgets(..., stdin)
您不需要打开 stdin
,因为 C 运行时启动代码会为您完成。如果您需要从参数提供的文件中读取,您需要先使用 fopen() 创建一个新的输入流。