Trouble with base64 programme

Hello, I am trying to create a program that encodes the user's input then proceeds to decode it. It's been good until I reached the decoding stage. Instead of decoding the string it prints: Syntax error: "|" unexpected


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include<iostream>
#include<stdio.h>
#include<array>
#include<string>


std::string pipeCmd_str(std::string strcmd)
{
        std::array<char,80> buffer;
FILE *pipe = popen(strcmd.c_str(),"r");
if(!pipe)
{
        std::cerr << " Couldn't open the pipe to read";
      return NULL;
}
while(std::fgets(buffer.data(),80,pipe) != NULL)
{
std::cout << ": " << buffer.data();
}
pclose(pipe);
return buffer.data();
}


std::string Encoder(std::string Inp_text)
{
        std::string pCommand = "echo " + Inp_text + " | base64";


return pipeCmd_str(pCommand);
}

void Decoder(std::string Inp_text)
{

        std::string pCommand = "echo " + Inp_text + " | base64 --decode";
        pipeCmd_str(pCommand);

}

int main()
{
        std::string inp;
        std::cout<<"\nEnter text to be encoded into base64\n: ";
        std::getline(std::cin,inp);
        std::string encoded_str = Encoder(inp);

        Decoder(encoded_str);

return 0;
}



If someone could please help me that'd be much appreciated :)
Last edited on
0. Always post the FULL error message.
sh: 2: Syntax error: "|" unexpected
The 2 is a big big clue.

1. Format your code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include<iostream>
#include<stdio.h>
#include<array>
#include<string>

std::string pipeCmd_str(std::string strcmd)
{
  std::array < char, 80 > buffer;
  std::cout << "DEBUG:>>" << strcmd << "<<\n";
  FILE *pipe = popen(strcmd.c_str(), "r");
  if (!pipe) {
    std::cerr << " Couldn't open the pipe to read";
    return NULL;
  }
  while (std::fgets(buffer.data(), 80, pipe) != NULL) {
    std::cout << ": " << buffer.data();
  }
  pclose(pipe);
  return buffer.data();
}

std::string Encoder(std::string Inp_text)
{
  std::string pCommand = "echo " + Inp_text + " | base64";
  return pipeCmd_str(pCommand);
}

void Decoder(std::string Inp_text)
{
  std::string pCommand = "echo " + Inp_text + " | base64 --decode";
  pipeCmd_str(pCommand);
}

int main()
{
  std::string inp;
  std::cout << "\nEnter text to be encoded into base64\n: ";
  std::getline(std::cin, inp);
  std::string encoded_str = Encoder(inp);
  Decoder(encoded_str);
  return 0;
}


2. Add some debug output, like the line 9 I added.
Then you see this

$ g++ foo.cpp
$ ./a.out 

Enter text to be encoded into base64
: hello
DEBUG:>>echo hello | base64<<
: aGVsbG8K
DEBUG:>>echo aGVsbG8K
 | base64 --decode<<
sh: 2: Syntax error: "|" unexpected
: aGVsbG8K
$ 

Notice how the string printed between the >><< of the DEBUG contains a newline.
Also notice that line 2 of that output (see the error message) begins with the pipe character that the sh error message is complaining about.

In short, you're passing a string with a newline in it, and it's mangling your command string.
Thank you for your help!
Topic archived. No new replies allowed.