Sei sulla pagina 1di 8

Training on C Programming Date : 19

th
April 99
Topic : Input-Output Page No : 1 of 8
Input / Output
There are numerous librar functions a!ailable for I"O# These can be classifie$ into t%o
broa$ categories :
1. Console I"O functions :
&unctions to recei!e input from 'eboar$ an$ %rite output to $ispla#
2. &ile I"O :
&unctions to perform I"O operations#
Console I/O functions :
These functions can further classifie$ into t%o categories : formatte$ an$ unformatte$ console I"O functions#
The basic $ifference them is that the formatte$ functions allo% the input rea$ from the 'eboar$ or the
output $isplae$ on the $ispla to be formatte$ as per our re(uirements#
Formatted Functions Unformatted Functions
Type
Input Output Type Input Output
char scanf)* printf)* char getch)*
getche)*
getchar)*
putch)*
putchar)*
int scanf)* printf)* int - -
float scanf)* printf)* float - -
string scanf)* printf)* string gets)* puts)*
Formatted Console I/O functions :
&unctions printf)* an$ scanf)* fall un$er the categor of formatte$ console I"O functions#
The format string contains
characters that are simpl printe$ as the are #
con!ersion specifications that begin %ith + sign #
escape se(uences that begin %ith , sign #
&or e-ample consi$er the follo%ing e-ample :
main)*
.
int a!g/0123
float per/29#43
printf)5A!erage / +$,n67 Percentage / +f,n67 a!g7 per*3
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Console Input / Output functions
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : 4 of 8
=
escape se(uence
con!ersion specification
characters printe$ $irectl
The output of the program is
A!erage / 012
Percentage / 29#4999
Conversion specifications
The +$ 7 +f use$ in the printf)* are calle$ as con!ersion characters # The tell printf)* to printf the !alue of
a!g as a $ecimal integer an$ the !alue of per as a float#
Con!ersion specifications are mentione$ in the table
Sr.No. Character Arument type
1> $ $ecimal integer
4> i integer )octal or he-a or $ecimal*
0> o octal integer
1> -7 ? he-a $ecimal integer
@> u unsigne$
2> c character
A> s string of non-%hite space characters
8> f float
9> e7 B $ouble
19> g7 C $ouble
11> p !oi$ pointer
14> + print +
De can specif the follo%ing optional specifiers for the con!ersion specifications#
Specifier !escription
$$ Digits specifing the fiel$ %i$th
. Decimal point separating fiel$ %i$th from precision ) number of places after the
$ecimal points*
$$ Digits specifing the precision ) number of $igits after the $ecimal points*
- Einus sign for left Fustifing the output in the specifie$ fiel$ %i$th #
&ollo%ing e-ample sho%s the use of abo!e $escription #
main)*
.
int %eight/203
float num/1#401@3
prinitf)5Deight is +$ 'g,n67 %eight*3
prinitf)5Deight is +1$ 'g,n67 %eight*3
prinitf)5Deight is +-1$ 'g,n67 %eight*3
printf)5Num is +f,n67num*3
printf)5Num is +1#4f,n67num*3
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : 0 of 8
printf)5Num is +1#0f,n67num*3
=
The output is
Deight is 20 'g
Deight is 20 'g
Deight is 20 'g
Num is 1#401@
Num is 1#40
Num is 1#401
"scape Se#uence
The character after a bac'slash ),* smbol is calle$ as escape character : it causes an escape from the normal
interpretation of a string7 so that the ne-t character is recogniGe$ as one ha!ing special meaning #
In the e-ample abo!e ,n causes the output to be $isplae$ on the ne-t line #
&ollo%ing are the list of some of the escape characters :
"scape se#uence $urpose
,n Ne% line
,t Tab
,a :eep
,, :ac'slash
The function scanf)* allo%s us to enter the $ata from the 'eboar$ that %ill be formatte$ in a certain
%a #
The general form of scanf)* statement is as follo%s :
scanf )5 format string67 list of a$$resses of !ariables*3
&or e#g#
scanf)5+$ +f +c67 Hc7 Ha7 Hch*3
Note that %e are sen$ing the a$$ress of the !ariables )using the IHJ the Ia$$ressJ operator* to
scanf)* function #
The !alues that are supplie$ through the 'eboar$ must be separate$ b either blan')s*7 tab)s*7 or
ne%line)s* #
All the form specifications use$ for printf)* are !ali$ for scanf )* function as %ell #
sprintf%& and sscanf%& Functions
The sprintf)* function %or's similar to printf)* function 7 but instea$ of sen$ing the output to screen as
printf)* $oes7 this function %rites the output to an arra of characters# The follo%ing program illustrates this :
main)*
.
int F/193
char ch/JAJ3
float a/0#113
char strK09L3
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : 1 of 8
sprintf)str7+$ +c +f67F7ch7a*3
printf)5,n+s67str*3
=
The counterpart of sprintf)* is sscanf)* function # It allo%s us to rea$ characters from a string an$ to con!ert
an$ store them in C !ariables accor$ing to specifie$ formats #
The sscanf)* function comes in han$ for in-memor con!ersion of characters to !alues #
The usage of sscanf)* is same as scanf)*7 e-cept that the first argument is the string from %hich rea$ing is to
be ta'e place #
Unformatted Console I/O Functions :
scanf )* function rea$s the input from the 'eboar$ untill ou press the enter 'e # Mometime %e re(uire to
rea$ the input %ithout %aiting for enter 'e to be presse$ #
&ollo%ing functions can use$ to ser!e this functionalit :
getch)* :
This function returns the character that has been most recentl tpe$ #
getche)* :
This function returns the character that has been most recentl tpe$ an$ also echoes the character
tpe$ #
e#g
main)*
.
char ch7che3
ch/getch)*3
che/getche)*3
=
putch)* is the counterpart of getch)* # It $isplas the output on the screen#
e#g
main)*
.
char c/JAJ3
putch) c *3
=
gets)* recei!es a string from the 'eboar$ as in case of scanf)* # :ut it has follo%ing a$!antage o!er
scanf)* #
e#g#
main)*
.
char nameK@9L3
printf)5Bnter the name,n6*3
scanf)5+s67name*3
printf)I+s67name*3
=
The output is
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : @ of 8
Bnter the name
If entere$ name is Nont Oho$es
The printf)* result %ill be
Nont #
This is because scanf assumes the blan' as en$ of input string # Pence Oho$es $oes not get $isplae$ #
To o!ercome this $isa$!antage the function gets)* is use$ #
ets%& reads the strin until it does not read the enter from the 'ey(oard.
Puts)* functions e-actl opposite to gets)*# It outputs the string to screen #
main)*
.
char nameK19L3
puts)5Bnter name ,n6*3
gets)name*3
puts)5Bntere$ name is,n6*3
puts)name*3
=
Bnter name
Nont Oho$es
Bntere$ name
Nont Oho$es
:ut the limitation is onl one string can be rea$ at a time using gets)* #
File I/O
These functions can be broa$l classifie$ as :
1. Pigh le!el file I"O functions )stan$ar$ I"O functions*
2. Qo% le!el file I"O functions )sstem I"O functions*
)ih level I/O functions :
Openin A File
Dhen %e re(uest the operating sstem to open a file7 %hat %e get bac' is pointer to the structure
FI*" %hich contains the information about the file being use$7 such as its current siGe7 its location
in memor etc# Eore importantl it contains a character pointer %hich points to the character that is
about to get rea$ #
Pence before opening a file %e $eclare
&IQB Rfp
fp/fopen)5abc#c676r6*3
No% fopen %ill open a file 5abc#c6 # A$$ress of the structure &IQB correspon$ing to file 5abc#c6
%ill then be assigne$ to !ariable fp#
After opening the file 7 it sets up the character pointer )%hich is part of &IQB structure * %hich
points to the first member of the chun' of the memor %here file has been loa$e$#
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : 2 of 8
If the file is absent7 fopen)* returns a NSQQ pointer in$icating that file is unable to open #
File openin modes
In the abo!e e-ample the string 5r6 in$icates the mo$e of the file operation#
&ollo%ing is the list of file opening mo$es an$ there correspon$ing meanings :
fopen%& mode Operations possi(le
5r6
Oea$ing from the file
5%6 Driting to the file7 create if the file is not a!ailable
5a6 Appen$ the ne% contents at the en$ of the file#
5rT6 Oea$ing the e-isting contents 7 %riting"mo$ifing the e-isting
contents of the file#

+eadin From A File
The function fgetc)* is use$ to rea$ the contents from the file#
&or e-ample :
char ch3
ch / fgetc)fp*3
%here fp is the file pointer of the file %hich is alrea$ opene$#
fgetc)* rea$s a character from the current pointer position7 a$!ances the pointer position so that it
no% points to ne-t char #
Dhile rea$ing the file using the function fgetc)*7 if it encounters the en$ of file 7 it returns the macro
BO& in$icating that the en$ of file has been reache$#
,ritin To A File
The function fputc)* can be use$ to %rite the characters to the file # It %or's similar to putc)*7 but instea$
of %riting to $ispla7 it %rites to a file %hich has been alrea$ opene$#
main)*
.
char ch3
ch/JaJ3
fp/fopen)5abc#c676rT6*3
fputc)fp7ch*3
fclose)fp*3
=

This routine %ill %rite a character IaJ in the file 5abc#c6 #
Closin A file
&ile is close$ using the function fclose)*# Once the file is close$ 7 it is no longer been use$ for accessing
it using fgetc)*#
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : A of 8
e#g# fclose)fp*3
Strin I/O in the file
&unctions fgets)* an$ fputs)* are use$ to rea$ or %rite a string from the file #
The ta'es three arguments :
1. Character buffer %here actuall the string is to be put#
2. Ea-imum number of btes to be rea$ from the file
3. &ile pointer of the alrea$ opene$ file
e#g#
char sK499L3
&IQBR fp3
fgets)s7A97fp*

This %ill rea$ A9 characters from a alrea$ opene$ file pointe$ b fp 7 an$ put the contents in the buffer
s#
fgets)* %ill than put a I,9J at the en$ of the characters rea$ from the file
fputs)* %rites the string to a file %hich is a alrea$ opene$ file pointe$ b fp#
e#g
fputs) s7fp*3
Formatted file I/O
&or formatte$ rea$ing an$ %riting of characters7 strings 7 integers7 floats7 there e-ist t%o functions
fscanf)* an$ fprintf)*#
The %or' e-actl the same %a as scanf)* an$ printf)* but the first argument to these functions is
the file pointer of the alrea$ opene$ file#
e#g#
main)*
.
&IQBR fp3
char nameK19L3
int age3
float salar3
fp / fopen)5abc#c676r6*3
fscanf)fp76+s +$ +f67name7Hage7Hsalar*3
printf ),n6+s +$ +f67 name7 age7 salar*3
=
Note : The format in %hich the fscanf is $one must be present in the file on %hich the
fscanf)* function has been operating#
*o- *evel I /O
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999
Training on C Programming Date : 19
th
April 99
Topic : Input-Output Page No : 8 of 8
In a lo% le!el $is' I"O 7 $ata can not be %ritten as in$i!i$ual characters 7 or as strings or as
formatte$ $ata as is possible using high le!el $is' I"O functions #
There is onl one %a $ata can be %ritten or rea$ in lo% le!el $is' I"O functions Uas a buffer full
of btes #
open %&
Instea$ of returning a &IQB pointers as fopen)* $i$ 7 in lo% le!el $is' I"O open)* returns an integer
!alue calle$ 5file han$le6 # This is a number assigne$ to a particular file7 %hich is use$ thereafter to
refer to the file #
On error it returns V1#
The open)* function is e-presse$ as :
int file8han$le3
file8han$le / fopen) 5file8name67 open8mo$e*3
&ile open mo$es can be :
O8APPBND Opens a file for appen$
O8COBAT Cerates a ne% file for %riting
O8ODDO Cerates a ne% file for rea$ing H %riting
O8ODONQ; Cerates a ne% file for rea$ing
Btc###
read %&
The follo%ing statement rea$s the file #
btes / rea$)file8han$le7 buffer7@14*3
rea$)* ta'es three arguments :
1. file han$le
2. a$$ress of the buffer %here the $ata is to be rea$
3. Ea-imum number of btes to rea$
rea$)* returns the number of btes rea$ # In abo!e statement it number of btes rea$ has been
assigne$ to !ariable btes #
-rite % &
The snta- for the %rite)* is similar to rea$)* #
%rite)file8han$le7 buffer7 btes*
This %ill %rite 5btes6 number of btes in the file in$icate$ b file8han$le #
close%&
close)* function is use$ to close the file
close)file8han$le*3
"!ar"%%%"apps"con!ersion"tmp"scratch81"411209898#rtf :; : NC
< Copright nilesh chittal Inc 1999

Potrebbero piacerti anche