Files - Seneca

Download Report

Transcript Files - Seneca

Introduction to Programming
Using C
Files
Contents




2
Files
Working with files
Sequential files
Records
Files

Arrays can store many strings but
–
–

Files offer an alternative
–
–
–
3
They are limited by the amount of memory in the
computer
When your program stops the data is lost
Data is stored on disk
Available space is much larger
Data is retained even when your program stops
Files

A file is
–
–
–

A file can contain
–
–
4
A named area on a disk
Stored as a stream of bytes
Can be read and/or written by programs
Text data
Binary data
Working with Files

To access a file
–
–
–
5
A program uses the function fopen() to open a file
which establishes a connection to the file
The program uses the function fprintf() and
fscanf() to write to the file and read from the file
The program uses the close() function to write
any remaining data to the file and sever the
connection to the file
Working with Files

All of the file functions are declared in
–


When you open a file, it returns a data structure
which references the file
This must be stored as a file pointer
–

6
#include <stdio.h>
FILE *fp;
After the file is opened, this variable will be used to
indicate which file you want to access
The fopen() Function

fopen() is the function which opens a file
–


The first parameter is the name of the file
The second parameter is the mode
–
–
–
7
FILE *fopen(char filename[], char mode[]);
“r” to read from the file and fail if the file does not exist
“w” to write to a new file or to overwrite the data in an
existing file
“a” to append to the end of an existing file or to create a new
file and append onto it
The fopen() Function

If fopen() is successful, it returns
–

If unsuccessful, it returns
–

8
A pointer to a data structure describing the open
file
The value 0, defined as NULL in stdio.h
We normally compare the value returned to
NULL to make sure that the file opened
correctly before we proceed
Sequential Files

The files we will be dealing with are called
sequential since
–
–
–

9
Data is written to them as a continuous stream
We read from the beginning of the file
We write data at the end of the file
There is another type of file called random
access that lets us read or write at any
position in the file
fprintf() and fscanf()


These functions write to and read from files
They work just like printf and scanf except
–
–
–
They have an extra first parameter which points to
the file they should write to or read from
fprintf(FILE *fp, char format[], data…);
fscanf(FILE *fp, char format[], data…);
* See file_io_demo.c
10
fclose()

This function
–
–
–
–
–
11
Closes a file when we no longer need it
Ensures that all data is on disk before the file is
closed
Frees the data structures which reference the file
Might unlock the file on some operating systems
Should be called before your program terminates
Other File Functions

rewind(fp)
–

c = fgetc(fp)
–
–
12
Rewinds a file to the beginning so you can read it
again
Reads a single character or returns -1 (EOF) if at
end of file
It is best to declare c to be an int so that it is easy
to compare to EOF
Other File Functions

fgets(char buf[], int max, FILE *fp)
–

fputc(char ch, FILE *fp)
–

Writes a single character to a file
fputs(char str[], FILE *fp)
–
13
Reads an entire line from a file, including the
newline character
Writes a string to a file
Records and Fields

There are 2 types of files we use
–
Report files

–
Data files

14
Designed to be viewed by people or printed out
Stores data to be read by the same program or another
program
Data Files


Data files often repeat the same information over
and over
Consider a customer file
–
–
–
–

15
Customer number,
Customer name
Customer address
Customer balance
This information is repeated for every customer
Data Files

All of the information on one customer is
called
–

Each piece of information on one employee
is called
–
–
16
A record
A field
Eg. The customer name field
Data Files

There is no set way to organize such file but
one way would be
–
–

Each record is on a line by itself
Each field is terminated by a semi-colon
This would make our file look like this
2345;Bob Jones;94 Oak St;234.67;
9876;Joan Smith; 32 Elm St.;128.45
7744;Fran Fox;183 Pine Ave.;439.00
17
More Logical Operations

There is one more logical operator
–
–
–

Eg.
–
–
–
18
!
Not
This simply reverses true to false and false to true
!(x < y)
Is the same as
x >= y