views:

95

answers:

4

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:

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);
}
+1  A: 

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.

bta
A: 

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.

Jason R. Mick
Cool, did not know I could pipe in cmd.exe unless I was using somethign like powershell.
MykC
+1  A: 

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.

Jerry Coffin
I believe this is closer to what I am looking for. I have found an area through project propeties (Alt+F7), under configuration properties click Debugging, on the right hand side there is command arguements. I put straight text here like "1 2 3 4" and directed it to a text file, but nothing seemed to change.
MykC
+1  A: 

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
Mark Ransom