Vous êtes sur la page 1sur 11

C Programming

Chapter
p 9
Input/Output

1 C Programming Lecture Notes. Reference Textbook : A Book on C by Al Kelley and Ira Pohl
Chapter 9: Input/Output
y OBJECTIVE
y In this chapter, we explain how to use some of the input/output functions in the
standard library,including the functions printf() and scanf().
y General
G l file
f l input/output
/ is important in applications
l where
h ddata reside
d in ffiles
l
on disks and tapes. We will show how to open files for processing and how to use
a pointer to a file.

2 C Programming Lecture Notes


Chapter 9: Input/Output
y THE INPUT FUNCTION PRINTF()
y The function printf() delivers its character stream to the standard output file
stdout, which is normally connected to screen.
printf(“
f(“ she
h sells
ll %d %s
% ffor f ”,
” 99,
99 “sea
“ shells”
h ll ” , 33.77);
77)
control string : “ she sells %d %s for f ”,
other arguments : 99, “sea shells” , 3.77
y The expressions in the other arguments are evaluated and converted according to
the formats in control string and then placed in the out stream. Characters in the
control string that are not part of format are placed directly in the output
streamThe % symbol introduces a conversion specification, or format. A single
conversion specification is a string that begins with % and ends with a conversion
character.

3 C Programming Lecture Notes


Chapter 9: Input/Output
y THE INPUT FUNCTION SCANF()
y The function scanf() delivers its character stream to the standard output file stdin.
scanf(%c%c%c%d%s%lf ”,&a,&c,&c,&n,s,&x);
control string : “%c%c%c%d%s%lf ”
other arguments &a,&c,&c,&n,s,&x
y The other arguments
g followingg th control stringg consists of a comma-seperated
list of pointer expressions and adresses. Note that in the preceding example,
writing &s would be wrong; the expression s by itself is an adress.

4 C Programming Lecture Notes


Chapter 9: Input/Output
y THE INPUT FPRINTF(),
FPRINTF() FSCANF()
FSCANF(), SPRINTF()
SPRINTF(), SSCANF()
y The function fprintf() and fscanf() are file versions of printf() and scanf(),
respectively.
y The
Th identifier
d f FILE is defined
d f d in stdio.h
d h as a particular
l structure, withh membersb
that describe current state of a file. Also defined in stdio.h are three file pointers
stdin, stdout, and stderr.
Wi
Written in
i C N
Name R
Remark k
stdin standard input file connected to keyboard
stdout standard output file connected to screen
stderr standard error file connected to screen
y A statement of the form
fprintf(file_ptr,
p ( p control_string,g other_arguments)
g )
y writes to the file pointed to by file_ptr. The conventions for scontrol_string and
other_arguments conorm to those of printf()

5 C Programming Lecture Notes


Chapter 9: Input/Output
y THE INPUT FPRINTF(),
FPRINTF() FSCANF()FSCANF(), SPRINTF()
SPRINTF(), SSCANF()
fprintf(stdout) is equivalent to printf()
y In a similar fashion, a statement of the form
fscanf(file_ptr, control_string, other-arguments)
y reads from the file pointed to by file_ptr. In particular
fscanf(stdout) is equivalent to scanf()
y The function sprintf() writes to its first argument, a pointer to char(string),
instead of screen. Its remaining arguments conform to those for printf(). The
function sscanf() reads from its first argument instead of from keyboard. Its
remaining arguments conform to those for scanf()

6 C Programming Lecture Notes


Chapter 9: Input/Output
y THE FUNCTIONS FOPEN() AND FCLOSE()
y Abstractly, a file can be thought of as a stream of characters. After a file has been
opened, the stream can be accessed with the file handling functions in the
standard
t d d library
lib
y Files have several important properties: They have a name. They must be opened
and closed. They can be written or read from, or appended to it. When it is
opened we can have access to it at its beginning or end
end. To prevent acidential
misuse, we must tell the computer which of the three activies; reading, writing,
or appending we will be performing on it. When we are finished using the file we
must close it.

7 C Programming Lecture Notes


Chapter 9: Input/Output
y THE FUNCTIONS FOPEN() AND FCLOSE()
#include <stdio.h>
int main(void)
{
int a, sum=0;
FILE *ipf, *opf;
ifp=fopen(“my_file”, “r”);
opf=fopen(“outfile”,”w”);
}
y This opens two files in the current directory; my_file for reading and outfile for
writing
while(fscanf(ifp, “%d”, &a)
sum+=a;
fprintf(opf, ”The sum is %d. \n”, sum);

fclose(ifp)

8 C Programming Lecture Notes


Chapter 9: Input/Output
y THE FUNCTIONS FOPEN() AND FCLOSE()
y A function call of the form fopen (filename, mode) opens the file in a particular
mode and returns a file pointer. There are number of possibilities for mode
Mode
M d Meaning
M i
“r” open text file for reading
“w” open text file for writing
“a”
a open text file for appending
“rb” open binary file for reading
“wb” open binary file for writing
“ab” open binary file for appending
y Each of these mode can end with a + character. This means that the file is to be
opened for bot reading and writing

9 C Programming Lecture Notes


Chapter 9: Input/Output
y ACCESSING A FILE RANDOMLY
y The library functions fseek() and ftell() are used to access a file randomly. An
expression of the form
f ll(f l
ftell(file_ptr);
)
y returns the current value of the position indicator.The value represents the
number or bytes from the begining of the file, counting from zero. Whenever a
character
h is
i readd ffrom the
h fil
file, the
h systems iincrements the
h position
i i iindicator
di bby 11.
fseek(file_ptr, offset, place)
y sets the file position indicator to a value that represents offset bytes from place.
The value for the place can be 0, 1, or 2 meaning the beginning of file, the
current position, or the end of file, respectively.
y Functions fseek() and ftell() are guaranteed to work properly only on binary files.
In MS-DOS iff we want to use these h ffunctions, the
h ffile
l should
h ld bbe openedd in bbinary
mode.

10 C Programming Lecture Notes


Chapter 9: Iput/Output

END OF CHAPTER 9

REFERENCE TEXTBOOK: A BOOK ON C by AL KELLEY &IRA POHL

11 C Programming Lecture Notes