File Handling and Command Line Arguments
File Handling and Command Line Arguments
CREATING A FILE
Before starting the work with files, it is necessary to set up a buffer area known
as file buffer.
This file buffer is space in main memory and is used to store information
temporarily when information being transferred between memory and data file.
FILE * fvar;
Where FILE is the structure datatype in C which establishes the buffer area.
fvar is a pointer variable that contains the starting address of buffer area. This is
known as stream pointer of buffer variable.
One stream pointer is required for each file which we want to use whether we
want to read from the file or to write to the file.
Function Description
Mode Description
filename is the name of the file to be opened and mode specifies the purpose of
opening the file.
Closing a File-
The fclose() function is used to close an already opened file.
General Syntax :
int fclose( FILE *fp);
Here fclose() function closes the file and returns zero on success, or EOF if
there is an error in closing the file. This EOF is a constant defined in the header
file stdio.h.
void main()
{
struct emp e;
FILE *p,*q;
p = fopen("one.txt", "a");
q = fopen("one.txt", "r");
printf("Enter Name and Age:");
scanf("%s %d", e.name, &e.age);
fprintf(p,"%s %d", e.name, e.age);
fclose(p);
do
{
fscanf(q,"%s %d", e.name, e.age);
printf("%s %d", e.name, e.age);
}
while(!feof(q));
fclose(q);
getch();
}
In this program, we have created two FILE pointers and both are referring to the
same file but in different modes.
fprintf() function directly writes into the file, while fscanf() reads from the file,
which can then be printed on the console using standard printf() function.
The only difference they have is, when you open a file in the write mode, the
file is reset, resulting in deletion of any data already present in the file.
While in append mode this will not happen. Append mode is used to append or
add data to the existing data of file(if any). Hence, when you open a file in
Append(a) mode, the cursor is positioned at the end of the present data in the
file.
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.
if( argc == 2 ) {
printf("The argument supplied is %s\n", argv[1]);
}
else if( argc > 2 ) {
printf("Too many arguments supplied.\n");
}
else {
printf("One argument expected.\n");
}
}
It should be noted that argv[0] holds the name of the program itself
and argv[1] is a pointer to the first command line argument supplied, and
*argv[n] is the last argument. If no arguments are supplied, argc will be one,
and if you pass one argument then argc is set at 2.
You pass all the command line arguments separated by a space, but if argument
itself has a space then you can pass such arguments by putting them inside
double quotes " " or single quotes ' '.