如何解决与Windows中的管道一起使用时,STDIN,STDOUT无法流式传输正确的输出
平台:Windows
语言:C
我有两个程序:
- 程序1使用fread读取二进制
.jpg
中的mode(_setmode(_fileno(stdin),_O_BINARY),_setmode(_fileno(stdout),_O_BINARY)
文件,并将使用fwrite的文件输出到stdout。 - 程序2从stdout读取并输出到文件(以二进制模式完成的
read/write
)。但是我可以看到输出图像已损坏,与输入不一样。 我在powershell中执行了program1.exe | program2.exe
。
但是,如果我编写程序以二进制模式读取文件并将其以二进制模式写入另一个文件,则输出是相同的。仅当我使用stdin
和stdout
时才会出现问题。
程序1:
int main()
{
int mode,fd,ret;
errno_t err;
int a = _setmode(_fileno(stdin),_O_BINARY);
a = _setmode(_fileno(stdout),_O_BINARY);
//freopen("ooo","wb",stdout);
//char in[4096];
char in[4096];
char o[100];
int size = sizeof(in);
FILE* fpIn,* fpOut,* fp3,* fin;
//int a =_setmode(_fileno(stdin),_O_BINARY);
int x = sizeof(in);
fpOut = fopen("out","wb");
//int bb = _setmode(_fileno(fpOut),_O_BINARY);
fin = fopen("pic.jpg","rb");
//fin = fopen("in.txt","rb");
//a = _setmode(_fileno(fin),_O_BINARY);
//int length = fread(&in,sizeof(in),1,stdin);
int count = 0;
while ((count = fread(in,fin)) != 0)
{
fwrite(in,count,stdout);
fflush(stdout);
}
fclose(fpOut);
}
程序2:
int main()
{
int a = _setmode(_fileno(stdin),_O_BINARY);
a = _setmode(_fileno(stdout),_O_BINARY);
//freopen("ooo.txt",stdin);
char in[4096];
int o[100];
int size = sizeof(in);
FILE* fpIn,*fp3,*fin;
//int a =_setmode(_fileno(stdin),"rb");
a = _setmode(_fileno(fin),stdin);
int count=0;
while ((count = fread(in,stdin)) != 0)
fwrite(in,fpOut);
fclose(fpOut);
}
程序的要求是允许用户通过stdin输入任意输入并将其写入文件。
解决方法
编辑
已添加到评论中的对话,因为其相关性可能是 对OP很重要。注意当前编码
cmd.exe
程序的设置对于执行某些 您要的东西。该主题在in this very detailed answer.中进行了讨论, 用来解释为什么您看到自己看到的东西。
您唯一声明的要求是“程序的要求是允许用户通过stdin输入任意输入并将其写入文件。”
下面是使用fread
和fwrite
的示例,但除非您需要才能使用它们,否则请考虑一种更简单的方法...
从注释中借用,此示例是最简单的实现,完全可以实现您所描述的需求。 (并且已使用Windows-10进行了测试。)
<ctrl-c>
退出,然后读取文件out.txt
...
int main(int argc,char *argv[]) {
int c = 0;
FILE *fp = fopen(".\\out.txt","w");
if(fp)
{
while( ( c = fgetc(stdin) ) != EOF )
{
fputc(c,stdout); //out to screen
fputc(c,fp); //to file
}
fclose(fp);
}
return 0;
}
读取和输出二进制文件非常简单,只需更改fopen()
中的参数以进行二进制读/写,然后指向要传输的图像:
当然不需要使用<cntl-c>
,文件复制完成后程序将退出。
int main(int argc,char *argv[]) {
int c = 0;
FILE *fpIn = fopen(".\\so_1.jpg","rb");
FILE *fp = fopen(".\\out.jpg","wb");
if(fp)
{
while( ( c = fgetc(fpIn) ) != EOF ) { fputc(c,fp); }
fclose(fp);
}
return 0;
}
如果 要求您使用fread/fwrite
,那么这是一个非常简单的示例,它使用经过一些修改的代码来完成完全相同的任务:(删除了对{{1 }},setmode
大小与将包含的文件大小相同,已删除in
循环,请参见代码注释。)
while
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。