Cout and Cin on Linux - can't see console

I just switched from Windows to Linux, and I'm trying to create a simple application that opens a console, displays a message, and waits for a key to close. I created it on Windows and it works, then I just moved the files to Linux. Did not make any changes, just compiled it with g ++, and I did not get any errors. The problem is that on Linux (Ubuntu 12.04) I do not see the console, and some messages ask 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;
}

Windows, , . Linux, , . testfile.txt , cstdio , , , . , , Linux. , . , ? !

, g++ cpp: g++ -Wall -s -O2 test.cpp -o test

+3
4

Windows "" . . Windows, ++ , .

++ , UNIX- .

UNIX- "" () , .

, , X11, , X11 .

, , , .

, - xterm -e ./test

, - :

#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();
}

, -xterm, xterm.

N.B. , std:: <cstdio>

+7

Windows , , stdio . Linux , stdio ( , X, ~/.xsession-errors ). , stdio , .

+2

. , .

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

+1
source

instead

cin.ignore();

follow

cin.get();

in the directory in which you compiled, run the program in the terminal window with:

./test

then writes "Writing a file ..." and "Press any key to exit ..." to the standard output, and you need to press any key to interrupt the application.

0
source

All Articles