gpt4 book ai didi

c++ - 如何将消息发送到缓冲区并用前三个单词反向打印消息?

转载 作者:行者123 更新时间:2023-11-30 05:17:52 25 4
gpt4 key购买 nike

我希望将以下消息:“This is a message to buffer”打印为:“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

最佳答案

$ echo "This is a message to buffer" | awk '{t=$1;$1=$3;$3=t}1'

a is This message to buffer

请注意,反转奇数元素将使中心保持不变。这就是为什么这里不需要更改第二个字段。

关于c++ - 如何将消息发送到缓冲区并用前三个单词反向打印消息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42012573/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com