开发者

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

开发者 https://www.devze.com 2023-01-16 18:08 出处:网络
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 st

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 f开发者_Go百科ile call "inputsInts" it becomes "a.out" and to test input I would type:

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.

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

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


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 Cmd window after the name of the shell program that runs underneath it. The command window is located under Programs->Accessories in 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.

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


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.


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.

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.


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

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.

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

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

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

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

More info on your specific needs would be helpful.

0

精彩评论

暂无评论...
验证码 换一张
取 消