[英]Using Visual Studio 2010, how do I provide input to a Visual C++ Win32 Console Application?
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. 我正在使用Visual Studio 2010 Pro进行简单的C编程,我想知道如何无需手动执行即可向程序提供输入。 The enviroment I am used to working is your standard commandline Unix enviroment.
我习惯使用的环境是您的标准命令行Unix环境。 Once I compile a C file call "inputsInts" it becomes "a.out" and to test input I would type:
一旦我编译了一个名为“ inputsInts”的C文件,它就会变成“ 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. 在Visual Studio 2010中,我必须要做的就是如此。我希望能够提前在一个区域中输入内容,或者至少让它读取文本文件。 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.
显然,我无法通过手动输入来测试大量数据。此刻,我只是在VS2010中进行编码,并在Unix环境中进行大多数测试。 I would like to stay in the VS2010 enviroment until I am ready to do a final test in Unix.
在我准备好在Unix上进行最终测试之前,我想一直停留在VS2010环境中。
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);
}
The cmd.exe shell has a pipe operator that works similar to the Unix pipe operator. cmd.exe外壳程序具有类似于Unix管道运算符的管道运算符。 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.
在某些版本的Windows中,它具有一些怪异之处,但通常,您应该能够使用它执行许多相同的操作。
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.txt
as the arguments to supply to the program in the project's debug settings. 为了从VS IDE内部进行大致相同的操作,您可能需要将示例输入存储在文本文件中,然后指定诸如
< sample.txt
类的东西作为要在项目的调试设置中提供给程序的参数。
You need to create a "Console Application" when you start a new Visual Studio project. 启动新的Visual Studio项目时,需要创建“控制台应用程序”。 This will give you a program that runs from a Windows Command Prompt window, otherwise known as the
Cmd
window after the name of the shell program that runs underneath it. 这将为您提供一个从Windows命令提示符窗口运行的程序,该程序在其下方运行的Shell程序名称后也称为
Cmd
窗口。 The command window is located under Programs
-> Accessories
in Windows XP, not sure about other versions of Windows. 命令窗口位于Windows XP中的“
Programs
->“ Accessories
下,不确定其他Windows版本。 Once you open a command window, things will work similarly to what you're used to on Linux. 打开命令窗口后,其工作原理将与Linux上的类似。
cd MyProject
echo 1 2 3 4|.\MyProject.exe
MyProject.exe <input.txt
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). 但是,您可以使用管道(Windows中的共享内存)来实现类似的功能。 See here for a full explanation with examples from the folks at Microsoft Developer Network:
有关Microsoft开发人员网络中人员的示例的完整说明,请参见此处:
http://msdn.microsoft.com/en-us/library/aa365780%28v=VS.85%29.aspx http://msdn.microsoft.com/zh-cn/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/zh-cn/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. 有关您的特定需求的更多信息会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.