【发布时间】:2017-06-20 03:15:30
【问题描述】:
我希望将以下消息:“这是要缓冲的消息”打印为:“a is This message to buffer”。我知道我必须使用 awk ,但我不太确定我该怎么做...
代码如下:
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
FILE *fpo; //for writing to a pipe
char buffer[BUFSIZ+1]; //BUFSIZ defined in <stdio.h>
//Write buffer a message
sprintf(buffer, "This is a message to buffer\n");
fpo = popen ( "od -c", "w" ); //pipe to command "od -c"
//od -- output dump, see "man od"
if ( fpo != NULL )
{
//send data from buffer to pipe
fwrite(buffer, sizeof(char), strlen(buffer), fpo );
pclose ( fpo ); //close the pipe
return 0;
}
return 1;
}
以下是该程序的输出:
luisgeesb@luisgeesb-pc:~/460/lab4$ g++ -o pipe2 pipe2.cpp
luisgeesb@luisgeesb-pc:~/460/lab4$ ./pipe2
0000000 T h i s i s a m e s s a g
0000020 e t o b u f f e r \n
0000034
基本上我想要这样的东西:
luisgeesb@luisgeesb-pc:~/460/lab4$ g++ -o pipe2 pipe2.cpp
luisgeesb@luisgeesb-pc:~/460/lab4$ ./pipe2
0000000 a i s T h i s m e s s a g
0000020 e t o b u f f e r \n
0000034
【问题讨论】:
-
是否需要使用 awk?否则将文本读入
std::vector<std::string>>,在vector的一部分上使用std::reverse,然后再次输出。 -
这就是我的想法,只是在我将消息写入缓冲区之前反转字符串,但我想知道这是否可以用 AWK 完成。
标签: c++ awk operating-system pipe