Download as pdf or txt
Download as pdf or txt
You are on page 1of 47

CSD101: Introduction to

Computing and Programming


Lecture 18
Today’s Lecture…

• File handling
• Opening a File
• Reading from a File
• Writing to a File

• Strings
• Two-dimensional array of Characters
• Standard Library Functions
File

• When we want to store data in a way that can be accessed/displayed


any time
• That storage is in the form of File on the hard disk (usually)
File Operations

• Creation of a new file


• Opening an existing file
• Reading from a file
• Writing to a file
• Moving to a specific location in a file
• Closing a file
For File Handling…

• Data type: FILE


• File type: Text
• Functions:
• fgetc():reads characters from a file
• fputc():writes characters to a file
• fopen():to open a file
• fclose():to close a file
For File Handling…

• Data type: FILE


• File type: Text
• Functions:
• fgetc():reads characters from a file
• fputc():writes characters to a file
• fopen():to open a file
• fclose():to close a file

• Note: We perform any operation to a file by first assigning a pointer to it


Example: Reading an Existing File
Example: Reading an Existing File
File Opening Modes
fopen() returns NULL for trouble in opening
Example: Counting Characters, Spaces,\t & \n
Example: Counting Characters, Spaces,\t & \n
Example: Writing to a File
Example: Writing to a File
What else with Files…

• We have seen character I/O in files


• We can also have string I/O in files
• Not only characters, we can also have numbers or structures for I/O in
files
• Just like text files, we can also handle binary files
• ……..
Two-Dimensional Array of Characters or Array
of Strings
Two-Dimensional Array of Characters or Array
of Strings
Example: Array of Strings
Example: Array of Strings
Array of pointers to Strings
Array of pointers to Strings
Array of pointers to Strings
Array of pointers to Strings
Space in Memory

• In general, compared to an array of strings, an array of pointers to


string occupies less space.
Example: Array of pointers to Strings
Example: Array of pointers to Strings
Array of pointers to Strings

Limitation: Cannot receive the strings from keyboard


ERROR!!!
Use malloc()
Use malloc()
Example: Passing array of pointers to Strings to a
function
Example: Passing array of pointers to Strings to a
function
Example: Passing array of Strings to a function
Example: Passing array of Strings to a function
Few Points to Remember
• In general scanf() stops taking input upon encountering a whitespace,\t (tab), \n (newline).
• The scanf( ) can have a scanset, which defines a set of characters to be read by the scanf().
• It is represented by %[]. Inside [] we can specify one or set of characters (all are case
sensitive).
• When using a scanset, scanf( ) continues to read characters until it encounters a character
outside the scanset.
• Example, %[XYZ] tells scanf( ) to read only the characters X, Y, and Z

• To specify an inverted set, append ^ in front of the set.


• “%[^\n]s” instructs scanf() to stop taking input upon encountering ‘\n’
• The function gets() terminates with ‘\n’ or EOF but not safe (does not check memory overflow)
• Safe to use fgets()
Example: gets and fgets
Example: gets and fgets
Character-Handling Library (<ctype.h>)

• It includes several functions that perform useful tests and


manipulations of character data.
• Each function receives an unsigned char (represented as an int) or
EOF as an argument.
• EOF normally has the value –1.
Character-Handling Library (<ctype.h>) Functions
Character-Handling Library (<ctype.h>) Functions
Example: Functions isdigit, isalpha, isalnum and isxdigit

• Function isdigit determines whether its argument is a digit (0–9).


• Function isalpha determines whether its argument is an uppercase
(A–Z) or lowercase letter (a–z).
• Function isalnum determines whether its argument is an uppercase
letter, a lowercase letter or a digit.
• Function isxdigit determines whether its argument is a hexadecimal
digit(A–F, a–f, 0–9).
Example: Functions isdigit, isalpha, isalnum and isxdigit
Example: Functions isdigit, isalpha, isalnum and isxdigit
Example: Functions isdigit, isalpha, isalnum and isxdigit
Standard Input/OutputLibrary Functions
• The character and string input/output functions of the standard
input/output library (<stdio.h>) specifically for manipulating character
and string data.
Standard Input/OutputLibrary Functions
References

• C How To Program, Paul Deitel and Harvey Deitel


• Let Us C, Yashavant Kanetkar

You might also like