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.

11.1 Command-line arguments

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) { /* ... */ }

// 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:

11.2 File handling in C

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.

11.2.1 Need of files

The creation of files, also called data files are important due to following reasons:

There are two types of files you should know about:

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.

11.2.2 File creation

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:


PIC

Figure 11.1: Text file creating program



PIC

Figure 11.2: Running Text file creating program


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.


PIC

Figure 11.3: Text file read program



PIC

Figure 11.4: Running Text file read program


11.2.3 Creating cat command

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.


PIC

Figure 11.5: File concatenate program


11.2.4 Creating cp Command

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.


PIC

Figure 11.6: File copy operation