0

I have success running a program I wrote (with file extension .exe) from the Windows command line with either an integer parameter or a redirection to specify input from a .txt file. Is there any way to do both?

For instance, the same project in Linux accepts './a.out 1 < testfile.txt' so 1 is in the arg array and testfile.txt is redirected as input. The same input in Windows will not work. I have tried something like ./a.exe (1 & '< testfile.txt') with no luck.

Thank you for any and all helpful responses, Tyler

asked Feb 17, 2017 at 2:10
2
  • 2
    a.exe 1 <testfile.txt should work just fine. In what way, exactly, does it fail for you? Commented Feb 17, 2017 at 2:11
  • @IgorTandetnik it is failing on that input with an error, "Input or output cannot be redirected because the specified file is invalid." The same file can be specified for use with same program but when compiling with gcc instead of windows Commented Feb 17, 2017 at 6:13

2 Answers 2

2

This won't work:

a.exe 1< testfile.txt

because 1< is interpreted as "redirect standard handle #1". For most applications, this will work:

a.exe 1 < testfile.txt

(note the extra space!)

If your particular application chokes on the extra space, and for some reason you can't fix that, this is another option:

<testfile.txt a.exe 1
answered Feb 17, 2017 at 2:20
Sign up to request clarification or add additional context in comments.

12 Comments

I had already tried the one that you said should work, it did not. I tried it again just in case. The file is for sure in the same directory as the .exe. As for your other option, it might be a good one to try but I am debugging in Visual Studio and can only append command arguments. It works for debugging linux but not Windows. Thank you for the options to try.
I am debugging in Visual Studio - then why does your question say you are running your executable from the Windows command line? That's not the same thing!
When debugging in Visual Studio, the redirected input file should be in the project directory, not the same directory as the executable.
My question was on how to run it at execution because I already knew how to append the parameters in Visual Studio and assumed it would just be distracting. I hadn't considered that the input file wouldn't be in the same directory as the executable, since that is what was working for Linux. I tried putting the test file in the project directory, the executable directory, and adding a full file path to it, with no joy.
I hadn't seen this post before posting: stackoverflow.com/questions/14043986/… After adding the complete file path, with double-quotes around it, I got the file input to work. Thank you.
|
0

Try combining type command and pipe.

something like:

type testfile.txt | a.exe 11

You might have to tweek that. Can't test it here on linux :]

answered Feb 17, 2017 at 2:12

3 Comments

that's probably a good choice for execution, but for debugging I can only append arguments for the debugger to pass during execution
You can use this also during debugging... In VS project settings you can specify the complete program sequence to run at debug start...
@Kupto, that works in principle but is a bit tricky in practice, because it means you're debugging cmd.exe rather than your executable.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.