file c command line arguments
file c command line arguments
Files – Types of file processing: Sequential access, Random access – Sequential access file
- Random access file - Command line arguments.
Sequential access
In this type of files data is kept in sequential order if we want to read the last record
of the file, we need to read all records before that record so it takes more time.
Example:
1) fseek( p,10L,0)
0 means pointer position is at the beginning of the file,from this statement pointer
position is skipped 10 bytes from the beginning of the file.
2)fseek( p,5L,1)
1 means current position of the pointer position. From this statement pointer position
is skipped 5 bytes forward from the current position.
3)fseek(p,-5L,1)
From this statement pointer position is skipped 5 bytes backward from the current
position.
ftell()
It tells the byte location of the current position of the cursor in file pointer.
rewind()
It moves the control to the beginning of the file.
Here argc counts the number of arguments on the command line and argv[ ] is a
pointer array which holds pointers of type char which points to the arguments passed to the
program.
Example:
#include <stdio.h>
#include <conio.h>
int main(int argc, char *argv[])
{
int i;
if( argc >= 2 )
{
printf("The arguments supplied are:\n");
for(i = 1; i < argc; i++)
{
printf("%s\t", argv[i]);
}
}
else
{
printf("argument list is empty.\n");
}
return 0;
}
Remember that argv[0] holds the name of the program and argv[1] points to the first
command line argument and argv[n] gives the last argument. If no argument is supplied,
argc will be 1.