windows 使用 Visual Studio 2010,如何向 Visual C++ Win32 控制台应用程序提供输入?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3754772/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 15:17:15  来源:igfitidea点击:

Using Visual Studio 2010, how do I provide input to a Visual C++ Win32 Console Application?

cwindowsvisual-studio-2010unix

提问by MykC

I am using Visual Studio 2010 Pro for simple C programming, I would like to know how I can provide input to the program without having to manually do so. The enviroment I am used to working is your standard commandline Unix enviroment. Once I compile a C file call "inputsInts" it becomes "a.out" and to test input I would type:

我正在使用 Visual Studio 2010 Pro 进行简单的 C 编程,我想知道如何向程序提供输入而无需手动执行此操作。我习惯工作的环境是您的标准命令行 Unix 环境。一旦我编译了一个 C 文件调用“inputsInts”,它就会变成“a.out”,为了测试输入,我会输入:

The easy way

简单的方法

echo 1 2 3 4| ./a.out //to provide input
The number of ints input was 4 //output

The easier way

更简单的方法

more input.txt| ./a.out //to provide input
The number of ints input was 4 //output

The tedious way

乏味的方式

./a.out
//now I would manually type
1 2 3 4 s //in this case I have to type a letter to move on
The number of ints input was 4 //output

Hard is how I have to do it in Visual Studio 2010. I would like to be able to just input in an area the input ahead of time or at least have it read a text file. Obviously I can't test large sets of data by manually typing it in. At the moment I am just doing the coding in VS2010 and going to the unix enviroment to do most testing. I would like to stay in the VS2010 enviroment until I am ready to do a final test in Unix.

难的是我必须在 Visual Studio 2010 中做到这一点。我希望能够提前在一个区域输入输入或至少让它读取文本文件。显然我无法通过手动输入来测试大量数据。目前我只是在 VS2010 中进行编码,然后转到 unix 环境进行大部分测试。我想留在 VS2010 环境中,直到我准备在 Unix 中进行最终测试。

I have altered the question quite a bit since I first posted, so the original answers may seem off a bit. Again I appretiate everyone's time and help.

自从我第一次发布以来,我已经对这个问题进行了相当多的修改,所以最初的答案可能看起来有点不对劲。我再次感谢大家的时间和帮助。

This is just the simple code for an example: #include

这只是示例的简单代码:#include

int main ()  {
    int x, n = 0;
    while (scanf("%d", &x)==1)
        n++;
    printf("The number of ints input was %d\n", n);
    return(0);
}

回答by bta

The cmd.exe shell has a pipe operator that works similar to the Unix pipe operator. It has some quirks in some versions of Windows but in general, you should be able to do many of the same things with it.

cmd.exe shell 有一个管道操作符,其工作方式类似于 Unix 管道操作符。它在某些版本的 Windows 中有一些怪癖,但总的来说,您应该能够用它做许多相同的事情。

回答by Jerry Coffin

You can run your program pretty much the same way at the Windows command line, the only obvious difference being that you need to specify the correct executable name instead of a.out.

您可以在 Windows 命令行中以几乎相同的方式运行您的程序,唯一明显的区别是您需要指定正确的可执行文件名称而不是a.out.

To do roughly the same from inside the VS IDE, you'd probably need to store your sample input in a text file, and then specify something like < sample.txtas the arguments to supply to the program in the project's debug settings.

要从 VS IDE 内部执行大致相同的操作,您可能需要将示例输入存储在文本文件中,然后< sample.txt在项目的调试设置中指定类似参数以提供给程序。

回答by Mark Ransom

You need to create a "Console Application" when you start a new Visual Studio project. This will give you a program that runs from a Windows Command Prompt window, otherwise known as the Cmdwindow after the name of the shell program that runs underneath it. The command window is located under Programs->Accessoriesin Windows XP, not sure about other versions of Windows. Once you open a command window, things will work similarly to what you're used to on Linux.

当您启动一个新的 Visual Studio 项目时,您需要创建一个“控制台应用程序”。这将为您提供一个从 Windows 命令提示符窗口运行的程序,也称为Cmd在其下运行的 shell 程序名称后的窗口。命令窗口位于Programs->Accessories在 Windows XP 下,不确定其他版本的 Windows。一旦你打开一个命令窗口,事情就会像你在 Linux 上习惯的那样工作。

cd MyProject
echo 1 2 3 4|.\MyProject.exe
MyProject.exe <input.txt

回答by Jason R. Mick

In the Windows Shell (cmd.exe) you can use a pipe similar to linux for commands like

在 Windows Shell (cmd.exe) 中,您可以使用类似于 linux 的管道来执行诸如

dir|more

Outside the shell, you're talking about a GUI environment (as in Linux's GUI) so passing info from one program to the next will be a bit different.

在外壳之外,您谈论的是 GUI 环境(如在 Linux 的 GUI 中),因此将信息从一个程序传递到下一个程序会有所不同。

However, you can achieve similar functionality using pipes (shared memory in Windows). See here for a full explanation with examples from the folks at Microsoft Developer Network:
http://msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx

但是,您可以使用管道(Windows 中的共享内存)实现类似的功能。有关 Microsoft Developer Network 人员示例的完整说明,请参见此处:http:
//msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx

Or if you're feeling to lazy to poke around, here's an example of transactions on named pipes: http://msdn.microsoft.com/en-us/library/aa365789%28v=VS.85%29.aspx

或者,如果您不想四处闲逛,这里有一个命名管道上的事务示例:http: //msdn.microsoft.com/en-us/library/aa365789%28v=VS.85%29.aspx

...or you can simply dump and read from output files.

...或者您可以简单地转储和读取输出文件。

(Both of these methods are similar to those used with Linux programs)

(这两种方法都类似于Linux程序使用的方法)

More info on your specific needs would be helpful.

有关您的特定需求的更多信息会有所帮助。