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 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 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 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 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 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 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 1-8
11.1 Working with Strings Reading a String as Input Use dbinput along with strcpy to store keyboard input 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 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 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 1-12
11.1 Working with Strings Arrays of Strings Figure 11-9 A two-dimensional char array as an array of strings 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. 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 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 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 1-17
11.2 Introduction to File Input and Output Opening an Output File 1-18
11.2 Introduction to File Input and Output Writing Data to an Output File Closing an Output File 1-19
11.2 Introduction to File Input and Output Opening an Input File 1-20
11.2 Introduction to File Input and Output Reading Data from an Input File Closing an Output File 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 1-22
11.2 Introduction to File Input and Output Using Loops to Process Files Detecting the End of a File 1-23
11.3 Saving a Game s High Score 1-24
11.3 Saving a Game s High Score 1-25
Chapter 11 Strings and Files QUESTIONS? Addison Wesley is an imprint of 2010 Pearson Addison-Wesley. All rights reserved.