Cout and Cin in Linux - can't see the console

℡╲_俬逩灬. 提交于 2020-01-03 18:52:15

问题


I just moved from Windows to Linux and I'm try to create a simple application that opens a console, displays a message and wait for key press to close. I have created it on Windows and it works, then I just moved the files to Linux. Didn't make any change, just compiled it with g++ and I get no errors. The problem is that on Linux (Ubuntu 12.04) I can't see the console and some message asking me to press any key before closing. My code is as simple as this:

#include <iostream>
#include <cstdio>

int main() {
    cout << "Writing file...\n";

        FILE *myfile = fopen("testfile.txt", "w");
        fwrite("test", sizeof(char), 4, myfile);
        fclose(myfile);

    cout << "Press any key to exit...\n";
    cin.ignore();
    return 0;
}

On Windows, when I start the executable, the console windows will show me the messages and close when pressing any key. On Linux, when I execute the program I don't get anything. It does create the testfile.txt file and insert the text, so cstdio related functions does work, but I can't see any console with those messages and I don't understand why. Maybe I don't know how to open a simple executable on Linux. What I want is to double click on it and see a console with two simple messages. What do you thin I'm doing wrong? Thanks!

Also, I use g++ to compile the cpp file: g++ -Wall -s -O2 test.cpp -o test


回答1:


On Windows the "natural" form of an application is a GUI application. When you run a console application the system creates a window to run a console and runs the application in that window. That is done by Windows, it is not an inherent property of C++ and is not implied by the code you wrote.

C++ doesn't do this automatically and UNIX-like systems don't do this for you.

On UNIX-like systems the "natural" type of application is (arguably) a console application and you would typically run them from a console or terminal.

When you run your program the output goes to the terminal where your X11 session is running, but you don't see it because the X11 session is controlling your display.

So to get the behaviour you want, first open a terminal, then run the program.

To make the program run in a terminal, try running something like xterm -e ./test

To make that automatic you could kluge it with something like:

#include <iostream>
#include <string>
#include <unistd.h>
#include <cstdio>

int main(int argc, char** argv)
{
  if (argc > 1 && std::string(argv[1]) == "-xterm")
  {
    if (::execl("/usr/bin/xterm", "xterm", "-e", argv[0], (char*)NULL))
    {
      std::perror("execl");
      return 1;
    }
  }

  std::cout << "Writing file...\n";

  FILE* myfile = std::fopen("testfile.txt", "w");
  std::fwrite("test", sizeof(char), 4, myfile);
  std::fclose(myfile);

  std::cout << "Press any key to exit...\n";
  std::cin.get();
}

Now if you run the program with the argument -xterm it will run in an xterm.

N.B. I fixed your non-portable code to use std:: qualification on the names from <cstdio>




回答2:


Windows opens a console, since that's the only way that stdio will work on it. Linux doesn't since stdio can function without it (with output going to the X session error log, ~/.xsession-errors by default). If you want to have stdio accessible to the user then you need to open a terminal and shell first and run the executable in there.




回答3:


You haven't told your desktop environment to run the program in a terminal window. Your program also doesn't tell, it just writes to its standard output.

To see the standard output of the program, the best is to open a console, and type the name of the compiled program to run it. The standard output of the program will be displayed in the same window.




回答4:


instead

cin.ignore();

do a

cin.get();

in the directory you did the compilation, start the program in a terminal window with:

./test

it then writes "Writing file..." and "Press any key to exit..." on the standard output and you are required to press any key to terminate the app.



来源:https://stackoverflow.com/questions/13809611/cout-and-cin-in-linux-cant-see-the-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!