如何将消息发送到缓冲区并反向打印前三个单词的消息
How can I send a message to a buffer and print the message with the first three words in reverse?
我希望将以下消息:"这是要缓冲的消息"打印为:"a 是要缓冲的消息"。我知道我必须使用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 buffern");
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
$ echo "This is a message to buffer" | awk '{t=$1;$1=$3;$3=t}1'
a is This message to buffer
请注意,反转奇数元素将使中心保持在原位。 这就是为什么这里不需要更改第二个字段。
相关文章:
- 模板-模板参数推导:三个不同的编译器三种不同的行为
- 在 2D 向量中使用第三个 [ ] 有什么意义?
- 如何通过按下第三个窗口中的按钮,将QString从一个窗口获取到另一个窗口
- 如何知道n!是否可以表示为三个连续数字的乘法?
- 我有三个 getline,但是一旦编译,输入就太多了
- 我遇到了黑客排名中的问题"TWO STRINGS"的三个测试用例的分段错误。原因是什么?
- 有没有更好的方法对C++中的三个整数进行排序?
- 如何检查第三个 API 是否在 Linux 中为 c/c++ 程序创建了一个新线程?
- 为什么 getch 在按下函数或箭头键时返回三个值?
- 使用三个数字比较器进行排序
- 我想在C++代码中比较这三个术语
- 我的动态链接队列在同一输出流中调用时不正确地输出三个返回函数
- 我该如何编码,使计算机知道两个名称条目和三个名称条目之间的区别
- 为什么我需要三个嵌套的大括号来调用赋值运算符,将const引用到二维数组
- 如何通过通用引用或std::forward将这三个c++模板函数合并为一个
- 首先处理第二个和第三个堆与第一个和第二个堆之间的逻辑差异
- 异常处理:如果用户输入不是三个特定字符之一
- 为什么将三个变量与 == 一起比较会计算为 false?
- 重载运算符 new 和运算符 delete 带有三个参数
- 如何将消息发送到缓冲区并反向打印前三个单词的消息