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

Ty Deuty
  • 137
  • 1
  • 2
  • 11
  • 2
  • @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 – Ty Deuty Feb 17 '17 at 06:13

2 Answers2

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
Harry Johnston
  • 35,639
  • 6
  • 68
  • 158
  • 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. – Ty Deuty Feb 17 '17 at 06:10
  • *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! – Harry Johnston Feb 17 '17 at 06:22
  • When debugging in Visual Studio, the redirected input file should be in the project directory, not the same directory as the executable. – Harry Johnston Feb 17 '17 at 06:29
  • 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. – Ty Deuty Feb 17 '17 at 16:40
  • I hadn't seen this post before posting: http://stackoverflow.com/questions/14043986/debuging-with-visual-studio-using-redirected-standard-input After adding the complete file path, with double-quotes around it, I got the file input to work. Thank you. – Ty Deuty Feb 17 '17 at 16:50
  • Sorry to be irritable yesterday (I'd had a *very* long day!) but in the scenario described in your question file input redirection is handled by `cmd.exe` whereas in your actual scenario it is handled by the Visual Studio debugger. There's no reason to expect them to have exactly the same behaviour, and apparently they don't. Not to worry, as these things happen, but please do be more careful in future. – Harry Johnston Feb 17 '17 at 18:35
  • ... it *was* a good idea to simplify the question to avoid possibly irrelevant aspects. But you need to actually check to make sure you can still reproduce the problem. In this case, if you'd tried issuing your command from the command line you'd have discovered that it worked, and that the fact that you were using a debugger wasn't irrelevant after all. :-) – Harry Johnston Feb 17 '17 at 18:35
  • I had tried it from the command line separately and it didn't work, the key was the double quotes around the file path. The behavior in the program arguments and cmd.exe are equivalent as I had suspected. – Ty Deuty Feb 18 '17 at 03:54
  • You don't need quote marks at the command line unless the file name contains spaces. – Harry Johnston Feb 18 '17 at 05:59
  • For the benefit of anyone reading these comments, I would argue that what I tried did not have spaces but still required quotes. All that I have run into in IT would agree with you that you only need it with spaces so I can't explain this behavior. I also disagree with this being marked as a redundant question as I have yet to see another post asking how to use BOTH an argument and a file redirect in cmd.exe. – Ty Deuty Feb 18 '17 at 16:53
  • Agreed, that clearly isn't your problem. But I'm reluctant to leave the question marked as answered when it is unresolved; my answer didn't help, the quote marks *shouldn't* have helped, and I can't reproduce. Can you post the actual command line, including the name of the executable and input file? – Harry Johnston Feb 18 '17 at 21:31
  • ... and does the problem occur consistently, i.e., even with a different project containing nothing but a "hello world" program? – Harry Johnston Feb 18 '17 at 21:33
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 :]

Kupto
  • 2,802
  • 2
  • 13
  • 16
  • that's probably a good choice for execution, but for debugging I can only append arguments for the debugger to pass during execution – Ty Deuty Feb 17 '17 at 06:11
  • You can use this also during debugging... In VS project settings you can specify the complete program sequence to run at debug start... – Kupto Feb 17 '17 at 07:14
  • @Kupto, that works in principle but is a bit tricky in practice, because it means you're debugging `cmd.exe` rather than your executable. – Harry Johnston Feb 17 '17 at 18:37