[go: up one dir, main page]

0% found this document useful (0 votes)
11 views25 pages

File I/O

Uploaded by

Bahubali C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views25 pages

File I/O

Uploaded by

Bahubali C
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

File I/O

1
Files
• A file is an external collection of related data
treated as a unit.

• Since the contents of primary storage are lost


when the computer is shut down, we need files to
store our data in a more permanent form.

• Additionally, the collection of data is often too


large to reside in main memory at one time. We
must have the ability to read and write portions
of the data while the rest remain in the file.
2
Classes of Files
There are two broad classes of files:
Text Files: All the data are stored as
characters which must be converted to
internal formats when entered into
memory. Text files are organized around
lines which end with a new line(“|n”).

Binary Files: Store data in internal


computer formats, such as integer and
floating-pt numbers. Take less time to I/O
because no format conversion is necessary.
3
• Files are stored on auxiliary or secondary
storage devices. Two most common are
disk and tape.
• A buffer is a temporary storage area which
holds data while they are being transferred
to or from memory. Its primary purpose
is to synchronize the physical devices to
your program needs(e.g., more data can
be input at one time then your program can
use. The buffer holds extra data until you
are ready for it).

4
• These buffering activities are taken care of
by software know as device drivers or
access methods, which are provided by
the supplier of the Operating System you
are using.

5
Files and Streams
• The computer looks at input and output data,
whether from a physical device such as a
keyboard, or from secondary files, as a stream of
characters or bytes.

• Since files exist separately from our program and


computer, we must have some way to connect
them: we must create a linkage between the
external file and its usage in our program. In
C, this linkage is know as a file table.

6
• The term file table implies that several things are
stored. It contains ALL the info needed to
locate your file wherever it is stored outside of
the computer. It also contains info such as the
file name, the location of its file buffer, and the
current state of the file.

• We define a file table with the standard FILE


type. There will be a file table for each file that
our program will access.

7
File System Basics
• The header <stdio.h> contains:

– Three file pointers(stdin, stdout, stderr).

– Several interrelated functions.

– Each stream that is associated with a file has a


file control structure of type FILE.

8
The file pointers(stdin, stdout, stderr-
i.e., Tables) are automatically opened
when the program starts.

File tables are created that POINT to these


file streams.

9
Three File Pointers

stdin Standard input Connected


file to the
keyboard
stdout Standard output Connected
file to the
screen
stderr Standard error file Connected
to the
screen 10
fopen( ) Opens a file Commonly
fclose( ) Closes a file
putc( ) Writes a char. to a file used C file-
fputc( ) Same as putc( ) system
getc( ) Reads a character from a file
fgetc( ) Same as getc( ) functions
fgets( ) Reads a string from a file
fputs( ) Writes a string to a file
fseek( ) Seeks to a specified byte in a file
ftell( ) Returns the current file position
fprintf( ) Is to a file what printf( ) is to the console
fscanf( ) Is to a file what scanf( ) is to the console
feof( ) Returns true if end-of-file is reached
rewind( ) Resets the file position indicator to the begin of the
file
remove( ) Erases a file 11
Fflush() Flushes a file
The File Pointer
• In order to read or write files, your
program needs to use file pointers.
• A file pointer is a pointer to a structure of
type FILE.
• It points to information that defines various
things about the file, including its name,
status, and the current position of the
file.
FILE *fp;
12
Opening a File
• The fopen( ) function opens a stream for
use and links a file with that stream.
Then it returns the file pointer associated
with that file.

• General form:

FILE *fopen(const char *filename,


const char *mode);
13
• filename is a pointer to a string of
characters that make up a valid filename
and may include a path specification.

• mode determines how the file will be


opened.

• fopen( ) function returns a file pointer


which should not be altered by your code.

• If an error occurs when it is trying to open


the file, fopen( ) returns a null pointer. 14
Mode Meaning Legal
r Open a text file for reading values for
w Create a text file for writing Mode
a Append to a text filer
b Open a binary file for reading
wb Create a binary file for writing
ab Append to a binary filer
r+ Open a text file for read/write
w+ Create a text file for read/write
a+ Append or create a text file for read/write
r+b Open a binary file for read/write
w+b Create a binary file for read/write
a+b Append or create a binary file for read/write
15
FILE *fp;
if ((fp = fopen("test","w"))==NULL) {
printf(''Cannot open file.\n");
exit(1);
}
• The number of files that may be open at any one
time is specified by FOPEN_MAX. This value
will be at least 8.

• If, when opening a file for read-only operations,
the file does not exist ,fopen ( ) will fail.

• When opening a file using append mode, if the
file does not exist, it will be created. 16
When a file is opened for append:
- All new data written to the file will be
added to the end of the file.
- The original contents will remain
unchanged.
When a file is opened for writing:
- If the file does not exist, it will be
created.
- If it does exist, the contents of the
original file will be destroyed, and a
new file will be created. 17
• The difference between r+ and w+ is :

- r+ will not create a file if it does not exist;


however, w+ will.

- If the file already exists, opening it with w+


destroys its contents; opening it with r+ does
not.

18
Closing a File
• General form:

int fclose(FILE *fp);

Returns zero for a successful close.

Returns EOF if an error occurs.

Generally, fclose( ) will fail only when a disk has


been prematurely removed from the drive or the
designated file pointer is incorrect.
19
• fclose( ) closes a stream that was opened
by a call to fopen( ).

• It writes any data still remaining in the


disk buffer to the file and does a formal
operating-system-level close on the file.

20
• Failure to close a stream invites all kinds
of trouble, including :lost data, destroyed
files, and possible intermittent errors in
your program.

• It also frees the file control block


associated with the stream, making it
available for reuse.

21
Writing a Character
To a File
• putc( ) and fputc( )

• General form:

int putc(int ch, FILE *fp);

Returns the character written if successful.


Otherwise, returns EOF.
22
Reading a Character
• getc( ) and fgetc( )
• General form:
int getc(FILE *fp);
Returns EOF when the end of the file has
been reached.

do {
ch = getc(fp);

} while(ch!=EOF); 23
Why have different functions(getc, fgetc,
putc, fputc) that basicly perform the same?

Good question!

The answer lies in the history of “C” and


involves information beyond the scope of
this class.

24
/* KTOD: A key to disk program. */
#include <stdio.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{ KTOD TEST
FILE *fp; Reads characters from the
char ch; keyboard and writes them to a
disk file until the user types a
if(argc!=2) { dollar sign.
printf(''You forgot to enter the filename.\n");
exit(1);
} 25

You might also like