I am currently have a C program that asks the user for input prompts.
My program has a "Please enter: " as the user prompt.
I am currently trying to automate this with a bash script, but the input
ordering keeps getting messed up.
In my bash script, if I try to do echo "ls" | ./program_executable, I get
Program output
Please enter:
When what I want is:
Please enter: ls
Program output
Are there any other methods I could use to accomplish this?
Thank you.
fflush(stdout) after the printf() should solve the problem.
Related
I've got a C program that's supposed to work on command line interface taking few arguments - to do that I'm using Cygwin.
I want to know if it's possible to printf some prompts into command like - things like "Give a number:" before I get data (program needs to prompt for a couple of things) and get the final result redirected to file. Or if I'm redirecting to file I need to just resign myself to the fact that I can't print anything and everything goes into the file?
Essentialy I'd like something like this:
printf("Please enter any number:");//prompting for some variable
scanf("%d", &some_variable);
function_printing_its_output();//a nodescript function that at the end uses printf() to get results on screen
Now I was wondering if it's possible to make the first printf() prompt appear in command lie (possibly using different function if it exists) but have the function output go into the file (and I cannot directly make it print to file, the program needs to use ./program_name some_stuff file1 > file2 format to work redirecting to file2).
You could have your prompt go to stderr using fprintf(stderr, "...");.
– lurker
Seems like a basic idea: I want to print out a prompt for a mini shell I am making in C. Here is an example of what I mean for the prompt:
$ ls
The $ being the "prompt". This little mini shell I am making supports backgrounding a process via the normal bash notation of putting a & symbol on the end of a line. Like $ ls &.
My logic currently is that in my main command loop, if the process is not going to be backgrounded then print out the prompt:
if(isBackground == 0)
prompt();
And then in my signal handler I print out the prompt using write() which covers the case of it being a background process.
This works fine if the background command returns right away like with a quick $ ls &, but in the case of something like $ sleep 10 & the shell will look like it is being blocked as the prompt will not be printed out until it hits the signal handler.
I can't figure out how to fix this because I don't know when the background process will end which means that the signal handler somehow needs to be saying when to print the new prompt because if the background process happened to have an output, it would output and then there would no longer be a prompt.
How can I resolve this problem? Is there a better way to do this that I'm not thinking of that could resolve my problem?
I'm new to code blocks and I'm writing code that takes in a command line input such as a file name, but once I compile and run the Code the console prompts "press any key to continue" like always and I'm unable to type anything in the console? So I can I write in console making my code run.
CodeBlocks will run your executable file without arguments, so you'll probably want to do it yourself. Open a command prompt (cmd.exe) and invoke your program with the desired arguments: C:/path/to/your/project/bin/Debug/program.exe filename.
Alternately you can request the user input via scanf or similar.
Hope it helps!
You cannot pass commandline arguments to your program in the Code::Blocks
console, because Code::Blocks has already started your program when you
see the console. You need to specify any commandline arguments in the
the Project settings before you run it. Then, when the console appears,
your program will be running with the commandline arguments you have
specified.
To specify commandline arguments, select the Project menu on the
top menu-bar of the IDE. In the Project menu, select Set program's
arguments, enter the commandline arguments you want and then OK out.
Once your program is running in the console (with or without commandline
arguments), if it requests any input from the user, then you will be
able to type the required input in the console.
I want to know how to execute a command from a c program,on windows os.
To be more specific how to write a c program whose output will not be printed but directly goes to command prompt and get executed there? please help me
I think you need to use system() command in your C code.
For example:
system("pause");
where "pause" is the command to be executed in cmd.
reference: http://www.cplusplus.com/reference/cstdlib/system/
I hope i got your question right.
I'm not sure I understand the question correctly. But if I do, you're looking for the system() function.
I suspect what you are describing is the the back-ticks in shells in Linux/Unix.
However, I don't know how to do that in Windows.
Unix way
myprompt> `./a.out`
If the C program was basically: printf("ls -l .\n");, then this should list the files.
Is that what you wanted?
Like I said, I don't know how to do that in the Win Cmd Prompt, but maybe this clarifies your question.
Looks like you could try:
C:\MyDir> MyProgram.exe | cmd.exe /C
I'm currently working on a bash-like project. However, I need to test this project with several thousands tests, what cannot be checked manually. That's why I'd like to perform the tests automatically.
My program used fgets() to get user input. I know how to send arguments to program directly, but when the program is started, it displays the prompt of the program. How to cat a file containing tests on the stdin to make the program able to interpret tests? (and get the program output in the terminal).
Assuming all your test input is stored in a file called "test.input" (with each line being the complete input to the program) you can do the following in bash:
export LINE_NO=0
cat test.input | while read LINE; do echo "$LINE" | ./program > results.$LINE_NO;
LINE_NO=$(($LINE_NO + 1));
done
Basically, for each line in test.input, the above command will feed it to the program and store the results of the program in results.0, results.1, results.2, etc, where the number in results.number is the line number of the test.input file.
Hope this helps.