Command line input to user input in Xcode - C++ - arguments

I have a program that works off command line input (argc and argv) but I don't know how to do that as I'm using XCode. Is there any way for me to get the command line input as user input in the program (e.g. cin the command line input through the program instead) and somehow convert that to argc and argv so I can run the main function which requires argc and argv?

The 'xargs' command is used, in general, to convert a stream of data to argv-type arguments. I have no idea id there's a practical way to configure that in XCode.

If you want to get your input from a file, you could make use of this solution:
Redirecting I/O in Xcode 4
Note that adding file input (< file.txt) arguments by going to Product -> Scheme -> Edit Scheme, choosing the "Run ..." scheme and then the "Arguments" tab will not work, at least not in Xcode 4.6.3.

Related

Provide input to an .exe file from a text file

When I run an a.exe file as below it is running fine:
C:\forc>a.exe 'iss mac'
6
(Output is 6)
How to provide this input from a text file?
I tried the below but no luck:
C:\forc>a.exe < input.txt
C:\forc>a.exe 'input.txt'
Kindly help.
the only time that you will be able to pass file contents into a program is when the program accepts a filename in the command line arguments or if the program is designed in a way that lets it read all the contents of the standard input stream. For example, in C#, you would treat Console.In in the same way you would treat a file input stream (read lines, chars, etc)
in conclusion
the program must directly support consuming data from standard input in order to use the < redirection. Standard Input is NOT the same as a command line argument.
Whether you can send input to a.exe depends entirely on a.exe. You will need to read the documentation for a.exe, or ask its author, to determine whether what you want to do is possible.
From Alexei Levenkov answer :
Using command redirection operators
program.exe < input.txt > output.txt

How do I use additional binary, such as tee.exe, as command line args with Visual Studio?

While debugging I want to display console output both on console and save a backup in file.
Windows doesn't have tee, but you can add one. Say the folder is c:\bin\ and it works fine. And I have added it into system's PATH.
Problem is setting "[ ]| tee[.exe] output.txt" or " | tee[.exe] output.txt" won't work -- the output.txt is just nowhere to be found. I also tried to add the c:\bin\ path explicitly in VC Directories or environment under debugging and merge environment to be yes.
"> output.txt" works fine.
Anyone has any idea how I can resolve this? Many thanks!
I assume that you're putting the | tee.exe output.txt string in the project property "Debugging | Command Argument".
Unfortunately, that property only supports the redirection operators, not the pipe operator. If you have the | tee.exe output.txt string in the preoperty and run a program that dumps the command line arguments, you'll see that that information is just passed on as the arguments. The "Debugging | Command Argument" doesn't actually get processed by a full-fledged shell (such as cmd.exe) - it's just the IDE supporting some simple redirection (actually, it seems to support more than I expected):
From http://msdn.microsoft.com/en-us/library/kcw4dzyf.aspx:
You can use the following redirection operators in this box:
< file
Reads stdin from file.
> file
Writes stdout to file.
>> file
Appends stdout to file.
2> file
Writes stderr to file.
2>> file
Appends stderr to file.
2> &1
Sends stderr (2) output to same location as stdout (1).
1> &2
Sends stdout (1) output to same location as stderr (2).
You can have a limited version of what you're looking for by redirecting the program's output to a file using >> and using a tail-f command to display whatever gets added to the file. If you do this you'll probably want to call setvbuf( stdout, NULL, _IONBF, 0 ) first thing in main() so that I/O is unbuffered. Otherwise tail -f won't see it until the buffer gets flushed, and I imagine that you'd like to see each output operation as it occurs.
Another option is to crank the console window's "Screen Buffer Height" property up to a large number - one of the first things I do when I get a new Windows machine is set that value to 3000 or so - then debug the program normally and copy/paste the contents of the console window before it closes.
You better NOT use printf for this purpose. Instead, write your own function; taking formatted-input, like printf - having variable number of arguments (...). That function will use printf to display on console, get the buffer written on file, would send to output to debug window and all. You may customize it depending on Debug/Release build.
It may go like (may have some minor mistakes):
void PrintDebuggingInfo(const char* pFormatString, ...)
{
va_list arguments;
char OutputString[1024];
va_start(pFormatString, argument);
vsprintf(OutputString, pFormatString, argument); // Generate string
// Now use `OutputString` as you wish!
}
You may use other variant of vsprintf. Infact all formatted-functions use this function only!

How to automate application based on command line?

I have a Java based application which I run using command prompt. I am currently testing it manually. I want to automate the process. What tool or scripting should I use?
For example:
First I will run the java command to run the .java file.
My application will give me 5 options. For example
i. Add
ii. Subtract
iii. Multiply
iv. Divide.
It should select one of the options and add the number and verify the result with the expected result. All this will be done using command prompt.
I realize that you're asking for a command prompt solution, but you really should look into JUnit. It will be a much more maintainable solution for a larger variety of test conditions. If you use the command line, you'll have to parse all of the input and output. In order to truly test your code, you shouldn't have to go through the intermediate of the command line (stdin, stdout); you should have Java code that runs your Java code, since it can access the variables.
If you really are set on using the command line, you would have to give us more details on how your program runs. This is just a shot in the dark:
inputs.txt (These are the inputs that will be fed to your program)
Add
4
6
test.bat
#ECHO OFF
:: I just assume that your program prompts for input and waits for stdin
java YourMainClass < inputs.txt > testOutput.txt
:: Program prompts for an operation; "Add" is read from inputs.txt
:: Program prompts for values; "4" and "6" are read from inputs.txt
:: Program prints out the result, which is redirected to testOutput.txt
:: Now you have to read in the testOutput.txt file and look at the last line
FOR /F %%i IN (testOutput.txt) DO SET RESULT=%%i
:: The variable RESULT now contains your program's answer
:: Create a variable to compare your actual result with the expected result
SET EXPECTED=10
IF %RESULT% == %EXPECTED% ECHO "You were correct"
IF NOT %RESULT% == %EXPECTED% ECHO "You were not correct"
You need to use a batch file. Put your commands in a notepad file and save it as .bat.
If they are not native batch commands though you may need to change some things, but you would need to post you commands before I can confirm.
Standard input can be fed into java application. If you're using bash it can be done like the following:
java myapp.java < test1
Where myapp.java is your application with main-method, and test1 being your test input (a plain old textfile).

How can I pipe stdin from a file to the executable in Xcode 4+?

I have an mpi program and managed to compile and link it via Xcode 4.
Now I want to debug it using Xcode 4.
How can I pipe the standard input to the program from a file?
In terminal I would type
mpirun -np 2 program < input.txt
I am able to run the program defining a custom executable (mpirun) in the "Info" panel of the Scheme editor, I also know that I can pass arguments in the "Arguments" panel. But Xcode 4 does not seem to accept "< input.txt" as an argument, even if I check "Use custom working directory" + add the correct directory of the input script in the "Options" panel.
This article Says it is possible to use "< input.txt" as an argument, but I guess that worked in Xcode 2 or Xcode 3, but it does not seem to work in Xcode 4 anymore.
In Xcode 4.5.1:
Open the scheme editor (Product menu -> Edit Scheme...)
Select the Run Debug scheme
In the 'Info' tab panel, change the 'Launch' radio button selection from 'Automatically' to 'Wait for MyApp.app to launch'
Close the scheme editor
Press the Run button to build and run your target. (Xcode's status window in the toolbar will show 'Waiting for MyApp to launch')
Launch Terminal and cd to your built application's folder. (It will be something like /Users/user/Library/Developer/Xcode/DerivedData/MyApp-dmzfrqdevydjuqbexdivolfeujsj/Build/Products/Debug / )
Launch your app piping in whatever you want into standard input:
echo mydata | ./MyApp.app/Contents/MacOs/MyApp
Switch back to Xcode and the debugger will have detected your application's launch and attached to it.
Not wanting to awaken zombies, but this is still the top search result for the question.
I've managed to get this working in XCode 9.2 - but it should be backwards compatible. It uses a subshell and sleep in a pre-action script.
This will pipe from a file and stick cout to a file, and supports breakpoints.
(1) Add the following preaction.sh script into your project.
#!/bin/sh
exec > ${PROJECT_DIR}/tests/result.log 2>&1
(
sleep 1
${TARGET_BUILD_DIR}/${EXECUTABLE_NAME} < ${PROJECT_DIR}/tests/file
)&
(2) Then add it to your pre-actions on the Run menu.
(3) Ensure your run Waits for executable to be launched.
(4) Turn off code signing for your target!
Otherwise you will get an error "Message from debugger: unable to attach"
(5) Set breakpoints and run from Xcode as normal
That's all there is to it. The entire trick rests on getting the pre-action script to spawn off a sub-shell that itself delays the process until the run action is launched.
Another approach:
In the very beginning of your program, freopen input file for stdin.
freopen( "input.txt", "r", stdin );
Conditional macro may help you.
#ifdef DEBUG
freopen( "input.txt", "r", stdin );
freopen( "output.txt", "w", stdout );
#endif
Try the following pre-action run script:
mv program program.real
echo -e '#!/bin/sh\nmpirun -np 2 program.real < input.txt' > program
Maybe this will trick XCode into executing your program as you would like it to be executed.

Executable pipe input?

How to run executable in Xcode with piped input? For example:
echo "abc" | myexec
I know I can set arguments to my executable in the Executable [name] Info > Arguments tab, but there seems to be no option to prefix it or something.
I see "Use Pipe for standard input/output" in the General tab, but how to work with it? Doesn't seem to change a thing.
I'm using Xcode 3.2.6.
I don't know if this is the most elegant way, but could you have a Shell Script build phase that shunts the output of the first command into a file (echo "abc" > tempfile) and then call your executable with (myexec < tempfile)?
I haven't found much online yet besides this now-broken link that explains the use of "Use Pipe for standard input/output".

Resources