How to write in code blocks console? - c

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.

Related

printf() redirection to file in command line (Cygwin)

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

How do I copy everything from my terminal to a file including the stdout and the prompt using C?

I know how to get the stdout into a file using dup/dup2 system calls, but how do I get the entire output that would be normally shown on my terminal(including the prompt that says my username along with the $ symbol and the current working directory) to a file?
Yes you can, but this may be difficult in many details (depending on your expert level). For the shell to behave normally (I would mean exactly as in a terminal), then it needs to interact with a terminal (special system object). So you need to create a program that behave like a terminal, this what pseudo-terminals devices (/dev) are intended for. Read documentation about this to implement it but roughly, your application should behave like the user so should be connected to the slave side of the pseudo-terminal, and the shell to the master side of the pseudo-terminal. Then you can easily log real inputs made by the user and catch outputs made by the shell.
Can't comment cause of low reputation.
I would say there is no way to do that inside a code in C. Instead, you could use bash for example to redirect everything to a file, and leave the code in C as it is.
In this way you have all the info you want to save: prompt, current directory, call to the program (including flags), and of course the output of the program.
Well, you can do:
-For bash prompt PS1: Echo expanded PS1 (in case you want it expanded, if not there is a simple way to do it just echong PS1)
- For executed command: https://unix.stackexchange.com/questions/169259/how-to-capture-command-line-input-into-logfile-and-execute-it-at-the-same-time
- Standard output and error output: Redirect stderr and stdout in a Bash script
And that's all you want to capture, I think.
Look up the script command in Unix systems. If you want to capture all keyboard and std in/out for a command, use the script executable. If you want to see how it's done, look up the source.

Eclipse console not displaying stdout

Basically whenever there are command line arguments given to my app (using the box in Run/Run Configurations/Arguments), the Eclipse console will stop displaying the stdout (e.g. prtinf() commands) Is there any way to solve this? Am I inputting the command line arguments in the wrong place? (they are appearing in argv though)

Where to physically write commands for input redirection in C?

This might sound like a retarded question but I am just learning C and all the websites I looked at this showed you the command to do it (project < somefile.txt) but not where to do it. Does it go in my project somewhere or command prompt? And if it is command prompt how do I get to where I need to enter it?
It goes on your command prompt. You first need to cd into location where your compiled binaries are. That command basically says "run project and feed contents of somefile.txt into it"
If you're on Windows hit Win+R and run cmd. On UNIX you should find terminal app somewhere in your menu.
Write your C code to use the built in FILE pointers defined in stdio.h. Read from STDIN and write to STDOUT. When you run your command like this: project < somefile.txt, the shell will open somefile.txt as STDIN before your program runs.

Are argc & argv useless in Windows?

In linux, argc and argv count arguments in terminal. But in Windows, I can't find anywhere to put the second arguments. Actually every time when I run the program, it creates that ugly black window and I even have no chance to give any argument. So are these two variables useless in Windows platform? Or there's another way to use it?
If you start from the command prompt, you can give the arguments there.
Start menu -> run -> "cmd" -> "MyProgram.exe arg1 arg2"
If you start your program from the command line as in linux, you are still able to pass arguments. Also if you create a Shortcut to an .exe you can place arguments for it there.
The int main(int argc, char**argv) interface is defined by C standards, so if Windows claims to have a ISO C99 compliant language implementation it should accept them, at least on the command line.
argc and argv are very usefull if properly used as arguments of main(). Probably the mistake (from my assumption) you are doing is that you are double clicking the executable rather than running the exe in the console and hence you are not getting a chance for entering the second argument.
In case you have document which states the proper use of the executable, then is should be followed and in case you have the source then check in that what arguments the main accepts and provide them in the command prompt after the name of the executable.
myexe.exe arg1 arg2
You can also use them in windows. As it needs command line interface you can use cmd.
Just compile your program and run your program on cmd like this: "filename" "argument1" "argumrnt2"...
For eg:look here
you can use argc argv[] in windows also one option is save your program in the bin folder of your c compiler may be turbo c++ or code blocks or whatever
I uses code blocks then open your cmd move to bin folder usind cd command then compile your program using command gcc *.c it will give you a file named a.exe like in linux it gives a.out the for run you can use command simply
a "argument1" " argument2"...... OR
a.exe "argument1" " argument2"......
i prefers first obviously every one prefers short command
second option is set environment variable of c by process given below
1.copy address of bin folder of compile
2.open properties of "This pc" left side press advance sysytem settings
3.press environment variable
4.under user variables press new
5.put varible name as Path p capital value paste the address copied from bin
press ok ok ok
now you can save your program anywhere and compile and run using cmd
__________****ENJOY****_________________

Resources