Cout和Cin在Linux - 看不到控制台 [英] Cout and Cin in Linux - can't see the console

查看:165
本文介绍了Cout和Cin在Linux - 看不到控制台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚从Windows移动到Linux,我试图创建一个简单的应用程序,打开控制台,显示消息,等待按键关闭。我在Windows上创建它,它的工作,然后我只是把文件移动到Linux。没有做任何更改,只是用g ++编译它,我没有得到任何错误。问题是,在Linux(Ubuntu 12.04)上,我看不到控制台和一些消息,要求我在关闭之前按任意键。我的代码很简单:

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;
}



在Windows上,当我启动可执行文件时,控制台窗口会显示我消息并按任意键时关闭。在Linux上,当我执行程序时我没有得到任何东西。它创建testfile.txt文件并插入文本,因此cstdio 相关函数可以工作,但我看不到任何控制台的消息,我不明白为什么。也许我不知道如何在Linux上打开一个简单的可执行文件。我想要的是双击它,看到一个控制台,有两个简单的消息。你瘦了我做错了什么?谢谢!

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!

此外,我使用g ++编译cpp文件: g ++ -Wall -s -O2 test.cpp -o test

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

推荐答案

在Windows上,应用程序的自然形式是一个GUI应用程序。当运行控制台应用程序时,系统将创建一个窗口来运行控制台并在该窗口中运行应用程序。这是由Windows完成的,它不是C ++的固有属性,并且不是由您编写的代码隐含。

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 ++不会自动执行此操作,系统不会为你这样做。

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

在类似UNIX的系统上,自然类型的应用程序是(可以说是)控制台应用程序,

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.

当您运行程序时,输出将发送到您的X11会话正在运行的终端,但是您不需要因为X11会话正在控制您的显示。

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.

要使程序在终端中运行,请尝试运行 xterm -e ./test

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

现在如果你运行参数 xterm 它将在xterm中运行。

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

NB我修复了您的非便携式代码,以对< cstdio>

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

这篇关于Cout和Cin在Linux - 看不到控制台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆