Chapter 11 Strings and Files. Starting Out with Games & Graphics in C++ Tony Gaddis

Similar documents
Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

by Pearson Education, Inc. All Rights Reserved.

Structured Programming Using C++ Lecture 10 : Graphics Programming with the Dark GDK Library. Dr. Amal Khalifa. Lecture Contents:

Chapter 12 Object-Oriented Programming. Starting Out with Games & Graphics in C++ Tony Gaddis

Chapter 8 The Vulture Trouble Game: Introducing Audio, Physics, and Text Effects. Starting Out with Games & Graphics in C++ Tony Gaddis

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

ONE DIMENSIONAL ARRAYS

C-String Library Functions

Important Questions for Viva CPU

Introduction to C/C++ Lecture 5 - String & its Manipulation

Iosif Ignat, Marius Joldoș Laboratory Guide 9. Character strings CHARACTER STRINGS

Computers Programming Course 10. Iulian Năstac

String Class in C++ When the above code is compiled and executed, it produces result something as follows: cin and strings

ARRAYS(II Unit Part II)

CSCI 6610: Intermediate Programming / C Chapter 12 Strings

Computers Programming Course 11. Iulian Năstac

Chapter 3: Modules. Starting Out with Programming Logic & Design. Second Edition. by Tony Gaddis

C-LANGUAGE CURRICULAM

CS5000: Foundations of Programming. Mingon Kang, PhD Computer Science, Kennesaw State University

Chapter 8 C Characters and Strings

SYSC 2006 C Winter String Processing in C. D.L. Bailey, Systems and Computer Engineering, Carleton University

Engineering Problem Solving with C++, Etter

Chapter 10. Arrays and Strings

C Characters and Strings

CS 115 Exam 3, Spring 2010

Week 8 Lecture 3. Finishing up C

Software Design & Programming I

Chapter 2: Input, Processing, and Output

C Style Strings. Lecture 11 COP 3014 Spring March 19, 2018

Chapter 8 Character Arrays and Strings

Midterm Sample Questions. a) What evaluates to FALSE in C? What evaluates to TRUE?

BITG 1113: Array (Part 2) LECTURE 9

CSC 2400: Computer Systems. Arrays and Strings in C

Fundamentals of Programming & Procedural Programming

LECTURE 15. String I/O and cstring library

Overview. Concepts this lecture String constants Null-terminated array representation String library <strlib.h> String initializers Arrays of strings

CSC 2400: Computer Systems. Arrays and Strings in C

C programming basics T3-1 -

Chapter 3: Decision Structures

Strings(2) CS 201 String. String Constants. Characters. Strings(1) Initializing and Declaring String. Debzani Deb

Aryan College. Fundamental of C Programming. Unit I: Q1. What will be the value of the following expression? (2017) A + 9

Procedural Programming & Fundamentals of Programming

(1-1) C Review: Pointers, Arrays, Strings, & Structs. Instructor - Andrew S. O Fallon CptS 122 (January 10, 2018) Washington State University

CCE1111 Programming for Engineers [C Programming Language]

C++ Arrays. Arrays: The Basics. Areas for Discussion. Arrays: The Basics Strings and Arrays of Characters Array Parameters

Chapter 5: Methods. by Tony Gaddis. Starting Out with Java: From Control Structures through Objects. Fourth Edition

M4.1-R3: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Model Viva Questions for Programming in C lab

C Libraries. Bart Childs Complementary to the text(s)

Introduction to Programming Using Java (98-388)

C mini reference. 5 Binary numbers 12

Chapter 9 Strings. With this array declaration: char s[10];

Write a C program using arrays and structure

Visual C# Instructor s Manual Table of Contents

Chapter 8: Character & String. In this chapter, you ll learn about;

Strings. Arrays of characters. Pallab Dasgupta Professor, Dept. of Computer Sc & Engg INDIAN INSTITUTE OF TECHNOLOGY

To declare an array in C, a programmer specifies the type of the elements and the number of elements required by an array as follows

Chapter 6: Arrays. Starting Out with Games and Graphics in C++ Second Edition. by Tony Gaddis

Loops / Repetition Statements

C strings. (Reek, Ch. 9) 1 CS 3090: Safety Critical Programming in C

A function is a named group of statements developed to solve a sub-problem and returns a value to other functions when it is called.

PES INSTITUTE OF TECHNOLOGY (BSC) I MCA, First IA Test, November 2015 Programming Using C (13MCA11) Solution Set Faculty: Jeny Jijo

9/9/2017. Prof. Vinod Mahajan

Midterm Exam 1 Solutions C Programming Dr. Beeson, Spring 2009

Data and Expressions. Outline. Data and Expressions 12/18/2010. Let's explore some other fundamental programming concepts. Chapter 2 focuses on:

Course organization. Course introduction ( Week 1)

This exam is to be taken by yourself with closed books, closed notes, no calculators.

CSC128 FUNDAMENTALS OF COMPUTER PROBLEM SOLVING Chapter 6: One Dimensional Array

8. Characters, Strings and Files

Shapes leading to CAD system project

Reading Assignment. Strings. K.N. King Chapter 13. K.N. King Sections 23.4, Supplementary reading. Harbison & Steele Chapter 12, 13, 14

COMP1917: 09 Arrays and Strings

Strings and Streams. Professor Hugh C. Lauer CS-2303, System Programming Concepts

C: How to Program. Week /May/28

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Introduction to Algorithms and Data Structures. Lecture 6 - Stringing Along - Character and String Manipulation

Object Oriented Programming COP3330 / CGS5409

Computer Programming. C Array is a collection of data belongings to the same data type. data_type array_name[array_size];

CS313D: ADVANCED PROGRAMMING LANGUAGE

Strings and Library Functions

Chapter 8 - Characters and Strings

Chapter 2: Using Data

Study Guide for Test 2

DR. A.P.J. ABDUL KALAM TECHNICAL UNIVERSITY LUCKNOW. Evaluation Scheme & Syllabus. For. B.Tech. First Year (Programming for Problem Solving)

C How to Program, 6/e by Pearson Education, Inc. All Rights Reserved.

1 Pointer Concepts. 1.1 Pointer Examples

Intermediate Programming, Spring 2017*

Software Design & Programming I

System Design and Programming II

Introduction to string

Scientific Programming in C V. Strings

Pointers, References and Arrays

Characters in C consist of any printable or nonprintable character in the computer s character set including lowercase letters, uppercase letters,

DATA STRUCTURES USING C

CSE 5A Final Fall 2006

CSI33 Data Structures

COMS W3101 Programming Language: C++ (Fall 2015) Ramana Isukapalli

Lecture 17. Strings II. CptS 121 Summer 2016 Armen Abnousi

Multiple Choice Questions ( 1 mark)

Grade Distribution. Exam 1 Exam 2. Exams 1 & 2. # of Students. Total: 17. Total: 17. Total: 17

Transcription:

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.