Anonymoose
Anonymoose

Reputation: 51

file as stdin in C in command line in visual studio

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?

Upvotes: 5

Views: 12902

Answers (6)

Marcel
Marcel

Reputation: 1

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.sstatic.net/ivjQC.png

Upvotes: 0

cdrini
cdrini

Reputation: 1096

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++)

Upvotes: 2

Gadzair
Gadzair

Reputation: 1241

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

Upvotes: 10

kanchirk
kanchirk

Reputation: 912

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

Upvotes: -1

Ferruccio
Ferruccio

Reputation: 100668

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.

Upvotes: 2

T.E.D.
T.E.D.

Reputation: 44804

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.

Upvotes: 0

Related Questions