【发布时间】:2013-10-19 03:26:06
【问题描述】:
我正在编写一个 C 程序来确定从标准输入读取的字节数。我 发现有一些方法可以为程序提供输入
- 管道输入
- 重定向
- 在程序等待输入时进入命令行
如何找到从 shell 执行程序的确切命令。 我尝试使用命令行参数但失败了。
#include <stdio.h>
int main(int argc,char *argv[])
{
char buffer[100];
int n;
for(n=1;n<argc;n++)
printf("argument: %s\t",argv[n]);
printf("\n");
if(argc==1)
printf("waiting for input :");
else if (argc==3)
printf("Not waiting for input . Got the source from command itself .");
n = read(0,buffer,100);
if(n==-1)
printf("\nError occured in reading");
printf("\nReading successfully done\n");
return 0;
}
还有,
【问题讨论】:
-
你忘记在
read之前调用fflush -
不清楚是否要计算程序或其他程序读取的字节数......