You are on page 1of 12

8

haracter Arrays
nd Strings
INTRODUCTION

i sSsequence
a of characters that is treated as a single
data item. We have used
c in a number of examples in the past. Any group of characters (except double quote
defined between double quotation marks is a string constant. Example:
"Man is obviously made to think."
liwe want to include a double quote in the string to be printed, then we may use it with a
slash as shown below.
" Man is obviously made to think," said Pascal."

Texample,
printf ("\" Wel1 Done "\");
output the string
Well Done!"
Ple the statement
Done ");
printf(" Well
Output the string
Well Done! and readable programs.
The com-

meaningful
aracter strings are often used to build
include:
Peratinngs
operations performed on character
strings

eading and writing strings.


Combining strings together
Copying one string to another.
nparing strings for equality.
Extract a portion of a string
nd examine
In this chapter we shall discuss these operations in detail and libr
examine li
that implement them. ibrary fur
8.2 DECLARING AND INITIALIZING STRING VARIABLES

However, it allows us to represp


us to
does not support strings as a data type. represen
character arrays. In C, therefore, a string variable is any valid C variah
formof
able nam string
ways declared as an array ofcharacters. The general declarationol aa
of declaration of n
is: string va
char string_namel size 1;

The size determines the number of characters in the string_namej Some examplesarea
char city[10];:
char name [30];
When the compiler assigns a character string to a character array, it automatical
plies a null character ('10 ) at the end of the string. Therefore, the size should be equalte
maximum number of characters in the string plus one.
Like numeric arrays, character arrays may be initialized when they are declared. (y
mits a character array to be initialized in either of the following two forms:
char city [9] NEMH YORK"
=
"

char city [9]={"N", "E', 'W*,'','Y, '0", 'R',


'K*, 10'};
The reason that city had to be 9 elements long is that the string NEW YORK contais
characters and one element space is provided for the null terminator. Note that when m
initialize a character array by listing its elements, we must supply explicitly the nul er
nator.
C also permits us to initialize a character array without specifying the number ufel
ments. In such cases, the size of the
array will be determined automatically, based on
number of elements initialized. For example,
the statement
char string [ ] =
{'G','0', '0', 'D', '\0'}
defines the array string five element array.
as a
We can also declare the size much itializer. Thati8
statement.
larger than the string size in the initializer.
char str [10] =
"GOOD";
permitted. In this case, the computer creates a character array of size 10, places theNUva
is NUL

GOOD" in it, terminates with the null character, and initializu3 all other elemene
The storage will look like:

Goo D10 10 10 o 10 10
However, the tollowing
declaration is illegal
char str2[3] "G00D"
TERMINAL
FROM
READING STRINGS
read in a
lsing s c a n l F u n c t i o n
format specification to
cs
be used with
function s c a n f
can
.e Lamiliar input
Example:
g of characters.
char add ress[10]
scanf("ss", address) ; e it
is that it terminates its input on the first white space
roblem with the scanf function tabs. carringe returns,
form feeds, and new lines.

white space includes blanks,


in thhe terminal,
at

t the following line of text is typed


NEW YORK
since the blank space a f t e r
estring "NEW" will be read into the array address,
ord NEW will terminate the reading of string.
he sc
with a null character
herf etion automatically terminates the string that is read
erefore the chara should be large enough to hold the input string plus
the
aracter. aracter
acter Note that unlike array
sand &)s previous scanf calls, in the c a s e of character arrays, the
1 not required before the variable name.
is not
Programming
in ANSI C

Functions
Using getchar and gets character from the term
how to read a single
Chapter 4 as to read successive si
We have discussed in We can use this function repeatedly to an entire
the functie getehar.
them into a
eharacter array.
Thus, line ofCha
text hara
ean be
ters from the input and place when the newline character
The reading is terminated
read and stored in an array.
inserted at the end of the
string. The geto
character is then
entered and the null
tion call takes the form:
char ch; t\u
Tvd U
ch getchar ();

function has no parameters.


Note that the getchar
read a line of fext conraining a Series of wOrdsir
Example 8.2 Write a program to
the terminal.
of text (up toa maximum of 80 charactersiin
The program shown in Fig. 8.2 can read a line
time a character is read, it is assigned tni
the string line using getchar function. Every
neuwline character. When the newline char
location in the string line and then tested for
is
ter is read (signalling the end of line), the reading loop terminated
and the newline cha
indicate the end of character string
acter is replaced by the null character to
When the loop is exited, the value of the index c is one number higher than the la
incremented after assigning the new char
character position in the string (since it has been
the null charate
acter to the Therefore the index value c-1 gives the position where
string).
is to be stored.
Program
#include <stdio.h>
main()

char 1ine [81], character;


cha int c
pu(eua thac C

printf("Enter text. Press <Return> at end\n") ;


do
h eha(
u-)
ckoatn character getchar();
P
p r(
u t" T
e h o( e h ) ;
1ine[c]

c++
= character;

while(character != 'In');
cC 1;
1ine[c]='\0';
printf("\n&s\n", 1line);
Fig.8.2 Frogram to redd a line
of text from terminal

oreconvenient method of reading a string of text containing whitespaces is


/more convenient

function getsavailablein the <stdio.h> header file. Thisis a simple func-


and called as under
tring parameter
Sh vou.
gets (str);
cea variable declared properly. It reads characters into str from the keyboard
ine
-ine character
character is encountered and then appends a null character to the string.

nf. it does not skip whitespaces. For example the code segment
char 1ine [80]
gets (1ine)
printf (*s", line)
e of text from the keyboard and displays it on the screen. The last two statements
bined as follows:

printf("s", gets(1ine));
not to input more character that can be stored in the string variable used. Since C
check array-bounds, it may cause
problems.)
notprovide operators that work on strings directly. For instance we cannot
g1o another assign
directly. For example, the assignment statements.
string "ABC";
valid. If we string1 =string2;
really want to copy the characters in string2 into
arader-by-character basis. stringl, we may do so
oe .3 Write a
we progre copied.
program
of characters
to copy one
string into another and count the number
ram is shown
into
into thewn in Fig. 8.3. We use a for
ke loop to copy the characters contained inside
are
againstring1. The loop is
assigning a null minated when the null character is reached. Note
character to the stringl
236 Programming in ANSI C

Program
main)

tu char stringl [80], string2[80


int i
printf("Enter a stringn");
printf("?") ;
Scanf ("%s", sting27;
for( i-0 : string2[il = '\0'; i++)
string1[i] =
stri ng2 [i];
string1[i] = '\0'

C)-sli) printf("\n"); w
printf("%s\n", stringl);
printf("Number of characters =
$d\n".
Totau
Sa utput
nter a string
?Manchester
ts
shi
Manchester

5,pnte Number of characters


Enter a strig
?Westminster
10
Sta
fe (1-0
Westminster
Number of characters 11
t
RA Hand st.

e p f f poute

FILE s7
AE data
ws we tyhe-stdio h

wh rulos

12
ile Marnagement
inC

2 INTRODUCTION

now we have been using the functions such as scanf and printf to read and
are console oriented lO functions, which always use the terminal (keyboarddata.
write
and
en) as the target place. This works fine as long as the data is small. However, many real-
problems involve large volumes of data and in such situations, the console oriented IO
rations pose two major problems.
It
becomes cumbersome and time consuming to handle large volumes of data through
terminals.
Iheentire data is lost when either the program is terminated or the computer is
turned off.
8therefore necessary to have a more flexible approach where data can be stored on the
and read whenever necessary, without destroying the data. This method employs the
eptof files to store data. A file is a place on the disk where a group of related data is
red a number of functions that have the ability to
d Like most
basic file
m
other languages,C supports
operations, which include:
naming a file,
opening a file,
'reading data fronm a file,
writing data to a file, and
closing a file.
ere are file operations is known as the
in C. The first one

eel 1/O and distinct


two
vo distinct ways to perform
wa second method is referred
to the high-levet
as

and uses UNIX System calls. The shall discuss in this chapter
peration
npor nd uses functions in C's standard
I/O library. We
are available
that
in the C library. They listed
l i in
are

121
le handling functions
ANSI C
Programming in
390
Level I/O Functions
Table 12.1 High

Function name
Operation
Creates a new file for use.
fopen) *Opens an existing
file for use.

*Closes a file which


has been opened for use.

fclose) from a file.


*Reads a character
getc) a file.
*Writes a characterto
putc() values to a file.
*Writes a set of data
fprintf) values from a file.
*Reads a set of data
fscanf) Reads an integer from a file.
getw) *Writes an integerto a
file.
putw) *Sets the position to a desired point in the file.
fseek) *Gives the current position
in the file (in terms of bytes frorm the start
file.
ftell) *Sets the position to the beginning of the
rewind)
functions. Not all ofthem are supported by all compilers. You should
There a r e many other
before using a particular
VO functior
check your C library

DEFINING AND OPENING A FILE


12.2
must specify certain things
want to store data in a secondary memory, we
file in the
Ifwe include:
about the file, to the operating system. They
1. Filename.
2. Data structure.
3. Purpose. the operating system.
It
that make up a valid filename for
Filename is a string of characters with the extension.
Exam-
name and a n optional period
may contain
two parts, a primary
ples:
Input.data
store
PROG.C
Student.c
Text.out /O function detn
of a file is defined as FILE in the library of
standard
Data structure
are used.
FILE 15
FILE before they
files should be declared as type
tions. Therefore, all
e
defined data type. must specify what we want to do with the file. For
example,
When we open a file, we data.
data to the file or read the already existing
may write format for declaring and opening a file:
Following is the general
FILE *fp
fp fopen ("fi lename". "mode");
type FILE". As
declares the variable fp as a "pointer to the data o n d s t a t e m e n t
The first
statement
The secondstatene
library.
is a cture that is defined in the I/O
FILE
stated earlier,
391
File Management in C
This
a med filename and assigns an identifier to the FILE type p o i n t e r fp.
pointer P
ype c
coor
rnnm u -
-

subsequently used as
used as a
ntains all the
a
he
pointer, w h i c h co information is subsequently
about the efile 1s
e
pens
terk hetween
the system and the program. this
The
the purpose of opening this file.T mode does
etatementthe
also specifies
i o nl i n k ,

Thes e c o n d

be
one of the fol
following:
can
Mode for reading only.
b the file
open

the file
for writing only.
w
o p e n

D the file
for appending (or adding) data to it.
ope in
be enclosed
that
both
the f ename and mode are specified as strings. They should
dN
o uo
b lteequotation marks,

ble
When
quorying to open a file, one of the following things may happen:
not
if the file does
mode is 'writing, a file with the specified n a m e is created
Whe hen the
The c o n t e n t s are deleted,
if the file already exists.
contents safe. A
is "appending, the file is opened with the current
exist.

When the purpose if the file does not exist.


Ele with the specified n a m e is created with the current
1s 'reading, and if it exists, then the file is opened
I fthe purpose
o t h e r w i s e a n e r r o r occurs.
contents safe
statements:
Consider the following end
FILE *pl, *p2; aluug
p1 fopen ("data", "r");
= c o l t

p2= fopen ("results", "w");


is for writing. In c a s e , the results
Thefile data is opened for reading and results opened file
is opened a s a n e w file. If data
its contents a r e deleted and the file
ilealready exists,
does not exist, an error will occur.
operation. They include:
Many recent compilers include additional modes of
both reading and writing.
The existing file is opened to the beginning for
and writing.
W*Same as w except both for reading
and writing.
Same as a except both for reading
time. This number however depends on thhe
and number of files at a
an
system
open
we use.
use a

12.3 CLOsING A FILE


Afile This ensures that
must be closed
all outsta e close all operations on it have been completed.the buffers and
a s soon a s
the file is tlushed out from al
inlks g information associated with accidental misuse of the file. In case, there is
nk to t nfile
are broken. It also prevents any
i t to of unwanted files
can be kept open simultaneously, closing
hthelnnumber of files that instance where we
have to close a file is when we
we
rpen the required files. Another
t to mode. The VO library supports a fun
anction to do
the file in a different
for usen
lor us. It
same
takes the following form:
fclose(file_pointer);
Gct e
392 Programming in ANSI Cc

This would close the file associated with the FILE pointer filepointer. Look
ok at
at the
the fo
lowing segment of a program.

FILE P NPUT"
p2 fopen ("OUTPUT", "");

fclose (p1);
fclose(p2);
This program opens two files and closes them after all operations on them are comnleted
Once a file is closed, its file pointer can be reused for another file. ted
As a matter of fact all files are closed automatically whenever a program terminatea
However, closing a file as soon as you are done withit is a good programming habit.

12.4 INPUTIOUTPUT OPERATIONS ON FILES


Once a file is opened, reading out of or writing to it is accomplished using the standard 10
routines that are listed in Table 12.1.

The getc and putc Functions


The simplest file I/O functions and
are getc pute. These are analogous to getchar and
putchar functions and handle one character at a fime. Assume that a file is opened with
mode w and file pointer fp1. Then, the statement
putc(c, fp1);
writes the character contained in the character variable to the file
c associated with FILE
pointer fp1. Similarly, getc is used to read a character from a file that has been opened n
read mode. For example, the statement
c getc(fp2) ;
would read a character from the file whose file pointer is
fp2.operation of gete or putC. u
The file pointer moves by one character position for every The
gete will return an end-of-file marker EOF, when end of the file has been reached.
fore, the reading should be terminated when EOF is Tnere
encountered.

Example 12.1| Write a


program to read data from the keyboard, write it to a file Co led
INPUT, again read the same data from the INPUT
the screen.
file, and dispiay

A program and the related input and output data are shown
in Fig. 12.1. We enter the T
data via the keyboard and the program writes it, character by
character, to the file IN the
The end of the data is indicated by entering an EOF character, which is
control-
reference system. (This may be control-D in other systems.) The file INPUT is closed a this
signal
File 393
Managunet in C

<Stdio.h

Data Input inin*);


Cpen re 1e INPUT /
open(INPUT". ' ) ;

Get a cnaracter from keyboard


'e((cegetcnar()) != E0F)

rite a character to INPUT


ute(c,f1): a chai
Close the fiie INPUT

se(f1);
nData Outputin\n");:
Reopen the file INPUT
fopen("INPUT",'r*);

Read a character from INPUT


i1e((c=getc(f1)) E0)
Display a character on screen
rintf(c",c):
C1ose the file INPUT
cose(f1)

input
program to test the file handling
tures on this
systemZ

Dutput
is
rogram to test the file handi ing
r
res on this system

Character oriented readiwrite operations


on a file
Fig. 12.1
Programming in ANSI C
394
The program then reads its content chas
The file INPUT is again reopened for reading.
ter by character, and displays it on the screen. Reading
1s
terminatted when gete encounto
te

the end-of-file mark EOF


Testing for the end-of-file condition is important. Any attempt to read past the end ofe-
with an e r r o r or result in an infinite loon s
terminate
might either cause the program to
ation.

The getw and putw Functions


funetions. similar to the gete and nue
They are
The getw and putw areinteger-oriented These funetions would be usef
functions and are used to read and write integer vatues.
forms of getw and putw are:
when we deal with only integer data. The general

putw(integer, fp);
getw(fp):
Example 12.2 illustrates the use of putw and getw functions.

numbers. Code a pro


Example 12.2 A file named DATA cOntains a series of integer
then write all 'odd' numbers to a file t
gram to read these numbers and
be called EVEN.
be called ODD and all 'even' numbers to a file to
and therefore, we nee-
The program is shown in Fig. 12.2. It uses three files simultaneously
to define three-file pointers f1, f2 and f3.
values read fror
DATA containing integer values is created. The integer
are
First, the file
of the statement
the terminal and are written to the file DATA with the help
putw(number, f1);
is closed. The nex
Notice that when we type -1, the reading is terminated and the file
step is to open all the three files, DATA reading, ODD and EVEN for writing. t
for Th-
and written
contents of DATA file are read, integer by integer, by the function getw (f1)
ODD or EVEN file after an appropriate test. Note that the statement

(number = getw(f1)) != EOF

end-of-file mark.
reads value, assigns the same to number, and then tests for the
a

Finally, the program displays the contents of ODD and EVEN files.
It is important
to
note
reopened
that the files ODD and EVEN opened for writing are closed before they
are

reading.

Program
#include <stdio.h>
main()

FILE f1, f2, *f3;


int number, i

printf("Contents of DATA file\n\n") ;

You might also like