T
T
Ternick2019-04-07 20:51:31
C++ / C#
Ternick, 2019-04-07 20:51:31

Why are there two newlines at the end of the function response and how to remove them?

THE CODE:

string python_shell(string code) {
  string command = "py -c " + code;
  FILE* fp = _popen(command.c_str(), "r");
  if (fp) {
    std::vector<char> buffer(4096);
    std::size_t n = fread(buffer.data(), 1, buffer.size(), fp);
    if (n && n < buffer.size()) {
      buffer.data()[n] = 0;
      return buffer.data();
    }
    _pclose(fp);
  }
}

And for any outcome, it gives out 2 empty lines at the end.
int main(){
    cout <<python_shell("print("")").c_str()<< endl;
    return NULL;
}

And the output is this:
ПУСТАЯ СТРОКА 1
ПУСТАЯ СТРОКА 2
КУРСОР

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman, 2019-04-07
@Ternick

the first '\n' is added by python print
the second c++ endl ;
python_shell is "a bit wrong".

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question