Chapter 11
Strings and Files
Starting Out with
Games & Graphics in C++
Tony Gaddis
Addison Wesley
is an imprint of
© 2010 Pearson Addison-Wesley. All rights reserved.
11.1 Working with Strings
• In C++, strings are commonly stored in char arrays
• char is a C++ data type for storing single characters in memory
The char Data Type
• char variable occupies one byte of memory
• Typically used to hold a single character
Copyright © 2010 Pearson Addison-Wesley
1-2
11.1 Working with Strings
The Way Strings Are Stored in Memory
•
•
Strings can vary in length
An extra byte, called a null terminator is appended to a string to indicate it’s
length
Figure 11-2 Character and String Storage
Copyright © 2010 Pearson Addison-Wesley
1-3
11.1 Working with Strings
Using char Arrays for String Storage
•
Strings are commonly stored in char arrays
•
Remember to declare an array large enough to hold the null terminator
Figure 11-3 A char array partially filled with a string
Copyright © 2010 Pearson Addison-Wesley
1-4
11.1 Working with Strings
Displaying a String Stored in a char Array
•
You can easily display a string that is stored in a char array with the
dbPrint, dbText, and dbCenterText functions
Using Standard C++ Library Functions to Work with Strings
Copyright © 2010 Pearson Addison-Wesley
1-5
11.1 Working with Strings
Using strcpy to Copy a String to an Existing Array
•
•
The srtcpy function copies a string to a char array
You cannot use the = operator to assign a string to an existing char array
Copyright © 2010 Pearson Addison-Wesley
1-6
11.1 Working with Strings
Using strcat to Append a String to an Existing Array
•
The srtcat function concatenates, or appends, on string to the end of
another
Copyright © 2010 Pearson Addison-Wesley
1-7
11.1 Working with Strings
Using strlen to Get the Length of a String
•
The srtlen function returns the length of a char array
Copyright © 2010 Pearson Addison-Wesley
1-8
11.1 Working with Strings
Reading a String as Input
•
Use dbInput along with strcpy to store keyboard input
Copyright © 2010 Pearson Addison-Wesley
1-9
11.1 Working with Strings
Comparing Strings
•
The srtcmp compares the
values of two char arrays
•
•
Returns a value as follows:
Array1 and Array2 are
both identical returns zero
• Array1 is less than Array2
returns negative number
• Array1 is greater than
Array2 returns positive
number
Copyright © 2010 Pearson Addison-Wesley
1-10
11.1 Working with Strings
The strstr Function
•
The strstr function searches for a string inside of a string
– Returns non-zero value if secondary string is found
– Returns zero if secondary string is not found
Copyright © 2010 Pearson Addison-Wesley
1-11
11.1 Working with Strings
Getting a File Name and Testing for the File’s Existence
•
The Dark GDK provides a
function called
dbFileExist that you
can use to determine if a
file exists before loading it
– Returns 1 (true) if file
exists
– Returns 0 (false) if file
does not exist
Copyright © 2010 Pearson Addison-Wesley
1-12
11.1 Working with Strings
Arrays of Strings
Figure 11-9 A two-dimensional char array
as an array of strings
Copyright © 2010 Pearson Addison-Wesley
1-13
11.2 Introduction to File Input and Output
Concept:
When a program needs to save data
for later use, it writes the data in a
file. The data can be read from the
file later.
Copyright © 2010 Pearson Addison-Wesley
1-14
11.2 Introduction to File Input and Output
•
•
Programmers usually refer to the process of saving data in a file as “writing
data” to the file
The term output file is used to describe a file that data is written to
Figure 11-12 Writing data to a file
Copyright © 2010 Pearson Addison-Wesley
1-15
11.2 Introduction to File Input and Output
•
•
The process of retrieving data from a file is known as “reading data” from the
file
The term input file is used to describe a file that data is read from
Figure 11-13 Reading data from a file
Copyright © 2010 Pearson Addison-Wesley
1-16
11.2 Introduction to File Input and Output
File Names
•
•
•
•
•
A file is identified by a file name
Each operating system has its own rules for naming files
Many systems, including Windows, support the use of file name extensions
File extensions are short sequences of characters that appear at the end of a
file name, preceded by a period (called a “dot”)
We will use the .dat file extension, which simply stands for “data”
Figure 11-14 Three files
File Numbers
•
•
•
•
When you use the Dark GDK to open a file, you assign a file number to the
file
A file number is an integer that you use to identify the file in subsequent
operations
File numbers must be in the range of 1 through 32
32 different files may be opened simultaneously
Copyright © 2010 Pearson Addison-Wesley
1-17
11.2 Introduction to File Input and Output
Opening an Output File
Copyright © 2010 Pearson Addison-Wesley
1-18
11.2 Introduction to File Input and Output
Writing Data to an Output File
Closing an Output File
Copyright © 2010 Pearson Addison-Wesley
1-19
11.2 Introduction to File Input and Output
Opening an Input File
Copyright © 2010 Pearson Addison-Wesley
1-20
11.2 Introduction to File Input and Output
Reading Data from an Input File
Closing an Output File
Copyright © 2010 Pearson Addison-Wesley
1-21
11.2 Introduction to File Input and Output
Determining if a File is Open
•
To determine if a file is opened you call the dbFileOpen function, passing
the file number as an argument
– Returns 1 (true) if the file is open
– Returns 0 (false if the file is not open
Copyright © 2010 Pearson Addison-Wesley
1-22
11.2 Introduction to File Input and Output
Using Loops to Process Files
Detecting the End of a File
Copyright © 2010 Pearson Addison-Wesley
1-23
11.3 Saving a Game’s High Score
Copyright © 2010 Pearson Addison-Wesley
1-24
11.3 Saving a Game’s High Score
Copyright © 2010 Pearson Addison-Wesley
1-25
Chapter 11
Strings and Files
QUESTIONS
Addison Wesley
is an imprint of
?
© 2010 Pearson Addison-Wesley. All rights reserved.