458 Programming and Data Structures
QUTJPUI i
Enter a file name: TEXT
PROGRAMMING IN C
Explanation In the above program the file is opened in read only mode. The read () declaration
within the w h ile loop reads single character from the file destined by the file handler s. Theputch
() statement following the read () declaration shows the read character on the console. The
w h ile loop ends when the end of file is detected.
d. Setting Buffer The size of buffer can be set using setbu f () function. This function is defined
in " s t d i o . h/f. The syntax of setbu f () is as follows.
Syntax void se tb u f( FILE *fp , char * b u f f e r ) ;
13.37 Write a program to set a buffer size using setbufO function.
# include <stdio.h>
# include <conio.h>
void main()
{
char bu ff [22] ;
c l r s c r ( ) ;
setbuf(stdout,buff);
printf (" tThis book teaches C ");
fflush(stdout);
Q12XELDT;
This book teaches C
Explanation In the above program a character array buff [22] is declared. The setbuf () function
sets the buffer size as per the size of bu ff [22] array. Theprintf () statement displays the message
written in it. If the characters written in the prin t f () statement are more than buffer size i.e. 22 the
program will be terminated with critical error. Hence, the text that is to be displayed using any output
function should be less or equal the size of buffer.
13.9 COMMAND LINE ARGUMENTS
An executable program that performs a specific task for operating system is called as command.
The commands are issued from the prompt of operating system. Some arguments are to be associated
with the commands hence these arguments are called as command line arguments. These associated
arguments are passed to the program.
In C language every program starts with amain () function and that it marks the beginning of the
program. We have not provided any arguments so far in the main ( ) function. Here, we can make
Files 459
arguments in the main like other functions. Themain () function can receive two arguments and they
are 1) argc 2) argv. The information contained in the command line is passed on to the program
through these arguments when the man () is called up by the system.
1. Argum ent argc An argument argc counts total number of arguments passed from command
prompt. It returns a value which is equal to total number of arguments passed through themain ().
2. Argum ent argv It is a pointer to an array of character strings which contains names of arguments.
Each word is an argument.
For example
Copy f i l e l fi le 2 .
Here, filel and file2 are arguments and copy is a command.
The first argument is always an executable program followed by associated arguments. If you do
not specify argument the first program name itself is an argument but the program will not run
properly and will flag an error.
Below given programs illustrate the command line arguments.
13.38 Write a program to display number of arguments and their names.
# include cstdio.h >
# include cconio.h>
main (int argc, char *argv[])
{
int x;
c lrs c r () ;
printf ("n Total number of arguments are %d n"jirgc);
for (x=Opc<argcpc++)
printf ("%st",argv[x]);
getchO;
return 0;
I
q it te u t
Total number of arguments are 4
C:TCC.EXE A B C
Explanation To execute this program one should create its executable file and run it from the
command prompt with required arguments. The above program is executed using following steps.
a) Compile the program.
b) Makes its exe file (executable file).
c) Switch to the command prompt. (C: TC>)
d) Make sure that the exe file is available in the current directory.
e) Type following bold line.
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.129.194.106