5

When I write a C program and compile it using a standalone compiler, such as MinGW, I can write "myprogram.exe < test.txt" and the standard input is test.txt.

How can I do that in Visual Studio 2010? I'm aware of "Command Arguments" in Project properties and then debugger, but I don't know what to type there. Is it just the path of the input file or something else?

BartoszKP
  • 34,786
  • 15
  • 102
  • 130
Anonymoose
  • 51
  • 1
  • 1
  • 2
  • possible duplicate of [Reading input from file in Visual Studio 2008](http://stackoverflow.com/questions/843342/reading-input-from-file-in-visual-studio-2008) – Fraser Jul 14 '12 at 03:29
  • possible duplicate of [Piping input into a c++ program to debug in Visual Studio](http://stackoverflow.com/questions/9613059/piping-input-into-a-c-program-to-debug-in-visual-studio) – Victor Sergienko Feb 06 '13 at 16:41

6 Answers6

10

Visual Studio does support this use-case. For your C++ project, go to properties, Configuration Properties, Debugging, and in Command Arguments type "< test.txt".

Gadzair
  • 1,221
  • 14
  • 21
2

Go to your project properties, and under "Configuration properties > Debugging", write something like the following in the "Command Arguments" field: < "$(TargetDir)\input.txt"

Note:

  • $(TargetDir) expands to the "Debug" folder of your project, so make sure input.txt is there. You can put any path here, but make sure it's absolute.
  • Be sure to use the double quotation marks, otherwise you'll get an error if your path contains spaces.

(using Visual Studio 2013 with C++)

cdrini
  • 989
  • 10
  • 17
2

You can't do that directly in Visual Studio. I/O redirection is a feature of the command processor, not VS. You can open a command prompt, navigate to the directory the executable lives in and issue the command:

myprogram.exe < test.txt

there (assuming test.txt is also in that directory, if not you can always use complete path names).

Update: You may be able to do what you want by having VS run the command prompt for you and start you program. Under Configuration Properties | Debugging, replace what's in the Command field (usually $(TargetPath)) with:

cmd.exe /c "$(TargetPath)" < source-file

Leave Command Arguments blank. I've never tried this, though. It might not work.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
  • Thanks for your reply! I do have a follow up question though. The main reason I want to use VS2010 is for the debugger. Is there any way for me to use a separate command prompt in conjunction with the debugger? – Anonymoose May 16 '11 at 18:05
0

That is a function of the command-line shell program, not the compiler. You can do exactly that, if you run under msys's bash instead of using the default (crappy) Windows DOS shell.

T.E.D.
  • 44,016
  • 10
  • 73
  • 134
0

In Visual Studio 2022 you can redirect input to your program by placing <test.txt in debugging\command argument. Just pay attention to where your file is placed. It will work only if your main and test files are in the same directory.

Look at which setting you are changing at the top of the window. I would suggest changing all configurations.

PS. After redirection, your console will be closed immediately so you have to use a debugger breakpoint to see the output. I was stuck at this point for an hour. This is amazing explanation how it works (Preventing console window from closing in Visual Studio)

https://i.stack.imgur.com/ivjQC.png

Marcel
  • 1
  • 1
-1

For a project within visual studio, Right Click on the Project and select Properties. Within the properties window you can type the absolute path of the physical file which you want to be used as input.

Else You should be able to run your app from console of Visual studio

Start->Programs->Microsoft Visual Studio 2xxx->Visual Studio Command Prompt

Program :

    class Program
{
    static void Main(string[] args)
    {
        if (args.Length > 0)
        {
            foreach (var item in args)
            {
                Console.WriteLine("{0}", item);
            }
        }
    }
}

Visual Studio 2010 Console Application Argument settings

kanchirk
  • 912
  • 2
  • 13
  • 29
  • Where do you specify that? I went Project Properties->Configuration Properties->Debugging and typed the file path in the Command Arguments category, but nothing was different. – Anonymoose May 16 '11 at 18:18
  • I wrote this small program. I am attaching the image in edit above. class Program { static void Main(string[] args) { if (args.Length > 0) { foreach (var item in args) { Console.WriteLine("{0}", item); } } } } – kanchirk May 16 '11 at 19:26
  • 1
    The settings for native languages (C & C++) are completely different than those for managed languages like C#. – Ferruccio Jul 17 '14 at 22:12