我正在尝试构建一个指令管道模拟器,我开始时遇到很多麻烦.我需要做的是从stdin读取二进制文件,然后在操作数据时以某种方式将其存储在内存中.我需要一个接一个地读取正好32位的块.
我如何一次读取32位的块?其次,我如何存储它以便以后操作?
这是我到目前为止所做的,但是检查我进一步阅读的二进制块,它看起来不正确,我不认为我正在读取我需要的32位.
char buffer[4] = { 0 }; // initialize to 0
unsigned long c = 0;
int bytesize = 4; // read in 32 bits
while (fgets(buffer, bytesize, stdin)) {
memcpy(&c, buffer, bytesize); // copy the data to a more usable structure for bit manipulation later
// more stuff
buffer[0] = 0; buffer[1] = 0; buffer[2] = 0; buffer[3] = 0; // set to zero before next loop
}
fclose(stdin);
Run Code Online (Sandbox Code Playgroud)
我如何一次读取32位(它们都是1/0,没有换行等),我将它存储在char[]哪里,可以吗?
编辑:我能够读取二进制文件,但没有一个答案产生正确顺序的位 - 它们都被破坏了,我怀疑字节顺序和读取问题并且一次移动8位(1个字符) - 这个需要在Windows和C上工作......?
这个问题来自我试图实施以下指令:
http://tldp.org/LDP/lpg/node11.html
我的问题在于:Linux管道作为输入和输出,但更具体.
基本上,我试图取代:
/directory/program < input.txt > output.txt
Run Code Online (Sandbox Code Playgroud)
在C++中使用管道以避免使用硬盘驱动器.这是我的代码:
//LET THE PLUMBING BEGIN
int fd_p2c[2], fd_pFc[2], bytes_read;
// "p2c" = pipe_to_child, "pFc" = pipe_from_child (see above link)
pid_t childpid;
char readbuffer[80];
string program_name;// <---- includes program name + full path
string gulp_command;// <---- includes my line-by-line stdin for program execution
string receive_output = "";
pipe(fd_p2c);//create pipe-to-child
pipe(fd_pFc);//create pipe-from-child
childpid = fork();//create fork
if (childpid < 0)
{
cout << "Fork failed" << endl;
exit(-1);
} …Run Code Online (Sandbox Code Playgroud)