Computer Fundamentals with C and Unix
(Command line arguments and files)
Lecture 11: Jan. 16, 2023 Prof. K.R. Chowdhary : Professor of CS
Disclaimer: These notes have not been subjected to the usual scrutiny reserved for formal publications. They may be distributed outside this class only with the permission of the Instructor.
It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.
The command line arguments are handled using main() function arguments where argc refers to the number of arguments passed, and argv[] is a pointer array which points to each argument passed to the program. Following is a simple example which checks if there is any argument supplied from the command line and take action accordingly.
The most important function of C is main() function. It is mostly defined with a return type of int and without parameters :
int main() { /* ... */ }
We can also give command-line arguments in C and C++. Command-line arguments are given after the name of the program in command-line shell of Operating Systems. To pass command line arguments, we typically define main() with two arguments : first argument is the number of command line arguments and second is list of command-line arguments.
int main(int argc, char *argv[]) { /* ... */ } or int main(int argc, char **argv) { /* ... */ }
argc (ARGument Count) is int and stores number of command-line arguments passed by the user including the name of the program. So if we pass a value to a program, value of argc would be 2 (one for argument and one for program name)
The value of argc should be non negative.
argv(ARGument Vector) is array of character pointers listing all the arguments.
If argc is greater than zero,the array elements from argv[0] to argv[argc-1] will contain pointers to strings.
argv[0] is the name of the program, After that till argv[argc-1] every element is command-line arguments.
// mainreturn.c #include <stdio.h> int main(int argc, char *argv[]){ printf("You have entered %d arguments\n", argc); for (int i = 0; i < argc; ++i) printf("%s\n"argv[i]); return 0; }
Other platform-dependent formats are also allowed by the C and C++ standards; for example, Unix (though not POSIX.1) and Microsoft Visual C++ have a third argument giving the program’s environment, otherwise accessible through getenv in stdlib.h.
Properties of Command Line Arguments:
They are passed to main() function.
They are parameters/arguments supplied to the program when it is invoked.
They are used to control program from outside instead of hard coding those values inside the code.
argv[argc] is a NULL pointer.
argv[0] holds the name of the program.
argv[1] points to the first command line argument and argv[argc-1] points last argument.
The process of file handling refers to how we store the available data or info in a file with the help of a program. The C language stores all the data available in a program into a file with the help of file handling in C. This data can be fetched/extracted from these files to work again in any program.
There are times when the output generated out of a program after its compilation and running do not serve our intended purpose. In such cases, we might want to check the program’s output various times. Now, compiling and running the very same program multiple times becomes a tedious task for any programmer. It is exactly where file handling becomes useful.
File handling is an essential task of the C programming language, and it has so many inbuilt functions which are related to print() and fscanf() functions like fopen(), fclose(), getw(), and putw(). To use these functions, we must learn about them very well.
The creation of files, also called data files are important due to following reasons:
When a program is terminated, the entire data is lost. Storing in a file will preserve your data even if the program terminates.
If you have to enter a large number of data, it will take a lot of time to enter them all.
However, if you have a file containing all the data, you can easily access the contents of the file using a few commands in C.
You can easily move your data from one computer to another without any changes.
There are two types of files you should know about:
Text files
Binary files
The text files are the most basic/simplest types of files that a user can create in a C program. We create the text files using an extension .txt with the help of a simple text editor. In general, we can use notepads for the creation of .txt files. These files store info internally in ASCII character format, but when we open these files, the content/text opens in a human-readable form.
Text files are, thus, easy to access as well as use. But there’s one major drawback; it lacks security. Since a .txt file can be accessed easily, information is not very secure in it. Added to this, text files consume a very large space in storage. The above drawback is overcome in binary files.
The binary files store information and data in the binary format of 0’s and 1’s (the binary number system). Thus, the files occupy comparatively lesser space in the storage. In simpler words, the binary files store data and info the same way a computer holds the info in its memory. Thus, it can be accessed very easily as compared to a text file.
Text files are the normal .txt files. You can easily create text files using any simple text editors such as Notepad. When we open those files, we will see all the contents within the file as plain text. You can easily edit or delete the contents. The text files minimum effort to maintain, are easily readable, and provide the least security and takes bigger storage space.
Binary files are mostly the .bin files in windows OS, while in Linux, they can have any name. Instead of storing data in plain text, they store it in the binary form (0’s and 1’s). They can hold a higher amount of data, are not readable easily, and provides better security than text files.
We can create a text file through program. The file is opened in write mode (”w”), and input is received from console using getchar() until ctril+D is typed, which stands for end-of-file character. On receiving this, the file is closed and program ends. Fig. 11.1 shows file creation program.
In C, we can perform four major operations on files, either text or binary:
Creating a new file
Opening an existing file
Closing a file
Reading from and writing information to a file
A created text file is read using a program as follows: a file is opened by fopen() function in read mode (”r”) and pointer is assigned to fpr FILE type pointer, which points to a file buffer in memory. The file is read char by char uisng fgetc(file pointer) command, until end of file character is read (i.e., ctrl +d), at this point the file is closed and program end. The file read char-by-char is also sent to screen through putchar(ch). This is shown in Fig. 11.3.
We can use the file read program to implement the “cat” command of Linux. We receive the command line argument 1 at command, open it as file name, the read this file and display its contents on screen. The program in Fig. ?? shows this.
We can create cp function through file handling program, where first file given in the command line argument is read and second file is written. This we can do by combining the programs shown in Figs. 11.3 and 11.1. This is shown in Fig. 11.6.