Sunteți pe pagina 1din 56

FILEHANDLING

Lecture 13, MATH153


What arestructures?
Allow user to pack variables of different data types under a
single name.

Syntax
CodeChallenge:Structure for aLINE
Structure fora LINE
A line has points, slope, midpoint, and distance between two
points.
Create a member variable of structure line.
Ask the x and y coordinates for eachpoint.
Quickrecap
Before we proceed,

Let’s have a quick recap on the standard input andoutput


operations we do in almost allprograms.

I/O functions
• scanf() and printf()
• getchar() and putchar()
• gets() and puts()
Drawback

• So far we have entered information into our programs viathe


computer's keyboard.

• This is somewhat laborious if we have a lot of data to process.

What canwe do to makeit more efficient?


Usefiles.
The solution is to combine all the input data into a file and let
our Cprogram read the information when it is required.

In this section, you will be able to..


• open a file for reading or writing
• read/write some contents of/to a file
• close a file
File
• A place on your physical disk where information isstored.
• Why do we need it?

Why files areneeded


Preserve data

• When a program is terminated, the entire data islost.


• Storing in a file will preserve your data even if the program
terminates.
Why files areneeded
Save time
• If you have to enter a large number of data, it will take a lot of
time to enter themall.
• However, if you have a file containing all the data, you can
easily access the contents of the file using few commands in C.
Moving data
• You can easily move your data from one computer to another
without anychanges.

Two types offiles


• Text files
• Binary files
Textfiles
• normal .txt files that you can easily create using Notepad or any
simple text editors.
• When you open those files, you'll see all thecontents within
the file as plain text
• You can easily edit or delete the contents.

Binaryfiles
• mostly the .bin files in your computer
• Instead of storing data in plain text, they store it in the binary
form (0's and 1's)
Files in C
• #include <stdio.h>
• FILEobject contains file stream information
• Special files defined in stdio:
• stdin: Standard input
• stdout: Standard ouput
• stderr: Standard error
• EOF: end-of-file, a special negative integerconstant
FileOperations
• Opening an existing file
• Creating a new file
• Closing a file
• Reading and writing information to a file
Opening afile
• Before we perform any operations on a file, we need toopen it.
• We do this by using a file pointer.

defined inside
stdio.h FILE
allows us to define

file pointer

• We use the function fopen() for opening the file.


Code
If we checkthe directory..

The file does exist.


Opening afile
• Before we perform any operations on a file, we need toopen it.
• We do this by using a file pointer.

defined inside
stdio.h FILE
allows us to define

file pointer

FILE* fopen(char *filename, char * mode)


FilePointer
FILE* fopen(char *filename, char * mode)

Name of the file mode of the file to be opened


Modes
FILE* fopen(char *filename, char * mode)

File Mode Description


r Open a text file
Name of the file
w Create a text file for writing, if it
exists, it isoverwritten
a Open a text file and append text to
the end of the file
rb Open a binary file for reading
wb Create a binary file for writing. If it
exists, it isoverwritten
ab Open a binary file and append data
at the end of thefile
Modes(cont’d)
FILE* fopen(char *filename, char * mode)
File Mode Description
r+ Open a text file in both readingand
writing mode
Name of the file
w+ Opens a text file for both reading
and writing.
It first truncates the file to zero
length if it exists, otherwise creates
a file if it does notexist.
a+ Opens a text file for both reading
and writing.
It creates the file if it does not exist.
The reading will start from the
beginning but writing can only be
appended.
fopen()
• If successful, returns a pointer to a FILEobject
• If fails, returns NULL
Closing afile

fclose() is used for closing opened files.


The only argument it accepts is a filepointer.
Closing afile

int fclose(FILE *fp);

• fclose() closes the file


• returns 0 on success, EOF if there’s an error closing the file.
Readingand Writing Operations in Files
getc() andputc()
getc() and putc() getchar() and putchar()

getc() and putc()

Reads a single character writes a characer to the file


from the file which has Identified by its secondargument
previously been opened

getc(in_file); putc(c, out_file); Note! File must be


opened in
either write or
append mode
Example
Create filepointer
Openfile
Write contents into the file
Closefile
Openfile
Print contents inside the file
Close file again
Output
fprintf() andfscanf()
fprintf()
• like printf()
• takes argument as file pointer

fscanf()
• like scanf()
• Takes first argument as filepointer
Whole code
Example of fprintf()
Create a filepointer.
Open thefile
Declare thenecessary variables
Askinput from the user
Write contents into the file.
Close the file
Output
fscanf()example
Create filepointer
Open thefile
Declare necessaryvariables
Read file content
Print file content after scanning
Close the file
Output
Example
Suppose we have a file input.txt with the followingcontent:

4kml6a2ktv9hk511q5o73a

Output: math153
SampleOutput
Appended

S-ar putea să vă placă și