What is difference between command line argument and scanf function in c?
1) what does command line argument mean?
2)If i can take input from user through scanf then what is the need of commad line argument.
3) what is principal difference between scanf and command line rgument
There are three types of standard inputs basically:
1.Compile time
2.Load time
3.Run time
1.Compile time: In this type the programmer himself gives input in code only while compiling.
2.Load time: Load time means when the program is being loaded into RAM for execution. In linux terminal in command prompt when you type ./a.out (Or any executable name) you are loading your executable file into RAM, which you've got after compiling it. So while loading the executable along with the executable whatever you are passing through command prompt is treated as command line arguments. And that can be used in code some where at run time. In short command line args is the input provided at load time.
3. Run time: Its the time while program is running or being executed, scanf () is one of the functions that can be used to provide input at run time. So with use of scanf () we can provide input to our program at run time basically.
They're two different ways of getting information into the program.
When you run a program with command line arguments, they're made available to the main function as parameters. Since they're C strings, you can read them as such. Running it with command line arguments is basically something like:
store picture_of_zx80.jpg myPornDirectory
That's running the store program with two arguments.
The scanf function, on the other hand, reads information in from standard input, something that needs to be provided separately to any command line arguments that may be provided.
A command line argument is added when you start the program.
e.g. notepad.exe myletter.txt
scanf reads information from the input pipe in other words after the program has started. it also can apply some formatting to the input data.
Command Line arguments are arguments you pass to your program when you start executing it, which then may be used in the program, for instance to control certain behaviour. They can be specified when you run it, for instance, if you had a program called test.exe, you could run it with
test.exe someArg
From the command line.
The scanf() function reads input according to what you specify from the standard input buffer stdin. In programs executed on the command line this is typically done to recieve user input, for instance like this:
int main(void) {
int input = 0;
scanf("%d",input);
printf("You inputted: %d",input);
return 0;
}
Related
I am trying to make a program that can process sentences in C in the POSIX environment. Assume that my program's name is "test". If the user entered just "./test", then my program will ask the user to enter some sentences. This one so far is easy.
However, if the user entered "./test < file.txt", the program should get the characters from that txt file. I do not know how I can get the characters of the file in C. I tried something like file = open(argv[2]);, but it did not work.
I will really appreciate it if you give me the answer to this question.
TL;DR: If you start your program like
./test
and you have to type in the input, then exactly the same program will read from file.txt if you start it as
./test < file.txt
Longer explanation starts here. (The following explanation is not 100% precise, but shall help to get an understanding what is going on in principle.)
In a C program you can open files with fopen. As a return value, fopen gives you a FILE pointer. However, when you start a program under Unix, three FILE pointers are already available. These default FILE pointers are stored in variables named stdin, stdout and stderr.
Of these, stdin can be used to read from, stdout and stderr can be written to. And, stdin is used as default in several C library calls, like, gets or scanf. Similarly, stdout is used by default for calls like printf.
Now, although they are called FILE pointers, they can in fact represent other things than just files. stdin could be a file, but it can also be a console where you can type in stuff.
This latter scenario is what you observe when you start your test program from the shell with the command
./test
In this case, the test process will be started with stdin just using the console from the shell from which you started the test program. Therefore, if in your test program you call, say, gets(), then your program will implicitly read from stdin, which represents the console input that was inherited from the shell. Consequently, in this case the user has to provide input by typing it in.
Now let's look at what happens if you start your process from the shell in the following way:
./test < file.txt
Here, the shell does a bit of extra work before it actually creates your test process. This is because the < file.txt part of your command line is interpreted by the shell - this is not passed as arguments to your program. Instead, what the shell does is, to open the file.txt and, when the test process is started, hand the opened file.txt over to the process such that in your test process stdin is connected to file.txt.
Then, the call to gets() in your program will again read from stdin, but this time stdin is not the console. This time stdin really corresponds to a file, that is, file.txt.
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
I am having issues with handling the arguments that are passed to my program. The user should have the ability to provide his own arguments or to provide a JSON file containing the arguments at launch.
The individual can enter their own arguments at the launch. For example:
prog --x 1 --y 2
The individual can also send the arguments through a file input (JSON).
prog < arguments.json
The issue I am having is trying to get to recognize if a file has been passed through stdin. More specifically, if the user runs the program without any arguments or file input.
I can easily find out if any arguments have been passed by checking argc, but then my program gets stuck at verifying if anything has been passed in stdin by going through this ;
[...]
char str[150];
char c;
while ((c = fgetc(stdin)) != EOF) {
strncat(str, &c, sizeof(char));
}
[...]
After launching the program with no arguments, it sits in the terminal waiting for my input (which it should not be doing since there is no file input). The problem is only when the user has put in no arguments at launch, and the program tries to check the stdin. Is there anyway for me to check if stdin is empty before using fgetc?
Not easily. There are two immediate problems.
As mentioned, it's not that easy to detect if the standard input is tied to a file. You could possibly use isatty but there may be perfectly valid reasons why you wish to send a file with real input to your program, not options.
As foreshadowed in the preceding point, if you use standard input for passing in arguments, that rather stops you from also using it for "proper" input. Now it may be that your program expects no input in which case that's fine. But, if you want to provide a file with options and allow input, things start to get difficult.
The way this is usually handled is to not use standard input for the options, rather you provide another option which will specify a file to be used.
For example, the file my.options could contain --x 1 --y 2 and you would run your program as one of:
prog --opts-file=my_options.json
prog -o my_options.json
That requires an extra level of indirection in that you have to handle both command line options and options read in from the file but it leaves standard input for what ${DEITY} intended :-)
I am trying to not hardcode the name of the input file in my C program. I have all of the other components working when I hardcode the filename. But would like to be able to pass it a string filename.
I am trying to execute compile a file called Matrix.c and name its executable matrix.
So, in terminal, when I get to my working directory.
gcc -g Matrix.c -o matrix
then when I compile
./matrix
It doesn't have a filename passed to it so I am gonna check for that and have the user input a filename to load.
However, when someone passes the filename, should it be passed as:
./matrix filename.txt
or
./matrix < filename.txt
With the latter option, I can't seem to get the name of the argument passed to the function from argv[1] — it's just "(Null)".
I know this is very simplistic question. But am I just completely off my rocker? Is it something to do with me running on OS X El Capitan. I know I've used the '<' convention before.
The issue is how the shell works, mainly. When you use:
./matrix filename.txt
then the program is given two arguments — the program name and the file name. When you use:
./matrix < filename.txt
then the program is given just one argument — the program name — and the shell arranges for its standard input to come from the file (and the file name is not passed to your program).
Either can be made to work; you just have to decide which you want to support. What should happen if the user types ./matrix file1.txt file2.txt file3.txt? One version of conventional behaviour would be to process each file in turn, writing each set of results to standard output. There are plenty of alternative behaviours — most of them have been used by someone at some time or another. Reading from standard input when there is no file name specified is a common mode of operation (think cat and grep and …).
Arguments to a command are in argv[1 .. argc-1].
The redirect from '<' sends the contents of the file to the program's stdin.
A third way to get the filename would be to print "Enter filename: " and then read the string typed by the user.
I have a compiled program that takes strings as input using stdin five times (in a loop).
Every time after taking input it does some work on it and gives an output.
I want to debug it gdb and I have to give it input from a file.
I cannot give input from keyboard directly.
I want to give the same string every time the program asks for input.
I have tried run < input.txt
But it seems to me that the program takes input from this file only the first time and after that it only reads null for other 4 inputs.
So I want to know if there is a way to solve this problem.
And am I right about the thing that input is read from file only the first time program asks for input and not read from the file rest of the times?
The run < input.txt and set args < input.txt and then run should work for all reads from stdin. Maybe you are onto the bug in your program that you are trying to find.