1 firstlastnextprevioushome richard jones 20031 COMP103A Introduction to Computer Science 1 Richard Jones G2.04

Slides:



Advertisements
Similar presentations
LECTURE 17 C++ Strings 18. 2Strings Creating String Objects 18 C-string C++ - string \0 Array of chars that is null terminated (‘\0’). Object.
Advertisements

CMSC 2021 C++ I/O and Other Topics. CMSC 2022 Using C++ Stream I/O Default input stream is called cin Default output stream is called cout Use the extraction.
CS 1620 File I/O. So far this semester all input has been from keyboard all output has been to computer screen these are just two examples of where to.
File I/O in C++. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk);is stored on a secondary storage device.
Chapter 10.
1 11/3/08CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 5: Loops and Files.
1 10/29/07CS150 Introduction to Computer Science 1 Reading from and Writing to Files Section 3.12 & 13.1 & 13.5.
Chapter 7. 2 Objectives You should be able to describe: The string Class Character Manipulation Methods Exception Handling Input Data Validation Namespaces.
1 File I/O In C++, I/O occurs in streams. A stream is a sequence of bytes Each I/O device (e.g. keyboard, mouse, monitor, hard disk, printer, etc.) receives.
Program Input and the Software Design Process ROBERT REAVES.
VARIABLES, TYPES, INPUT/OUTPUT, ASSIGNMENT OPERATION Shieu-Hong Lin MATH/CS Department Chapel.
CSIS 123A Lecture 6 Strings & Dynamic Memory. Introduction To The string Class Must include –Part of the std library You can declare an instance like.
Chapter 8 Data File Basics.
C++ Streams Lecture-2.
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
File I/O ifstreams and ofstreams Sections 11.1 &
Chapter 9 I/O Streams and Data Files
1 CS161 Introduction to Computer Science Topic #13.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
File Input and Output in C++. Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) Keyboard Screen executing program input data.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
File I/O 1 ifstreams and ofstreams Sections 11.1 & 11.2.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
TEXT FILES. CIN / COUT REVIEW  We are able to read data from the same line or multiple lines during successive calls.  Remember that the extraction.
C++ Streams Lecture-2. C++ Streams Stream  A transfer of information in the form of a sequence of bytes I/O Operations:  Input stream: A stream that.
 2003 Prentice Hall, Inc. All rights reserved. 1 Arrays Outline Introduction Arrays Declaring Arrays Examples Using Arrays.
Chapter 13 – C++ String Class. String objects u Do not need to specify size of string object –C++ keeps track of size of text –C++ expands memory region.
File I/O in C++ II. Open() function Open() is a member function in each classes ( fstream, ifstream, ofstream) Void fstream :: open ( const char *filename,
1 CISC181 Introduction to Computer Science Dr. McCoy Lecture 13 October 13, 2009.
FILE I/O IN C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
C++ Programming: chapter 6 – iostream 2014, Spring Pusan National University Ki-Joune Li 1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Loops and Files. 5.1 The Increment and Decrement Operators.
Chapter -7 Basic function of Input/output system basics and file processing Stream classes : I/O Streams. A stream is a source or destination for collection.
CS31: Introduction to Computer Science I Discussion 1A 5/14/2010 Sungwon Yang
CSC 143A 1 CSC 143 Introduction to C++ [Appendix A]
Chapter 15 Strings as Character Arrays
Chapter 11 Standard C++ Strings and File I/O Dept of Computer Engineering Khon Kaen University.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
A FIRST BOOK OF C++ CHAPTER 14 THE STRING CLASS AND EXCEPTION HANDLING.
C++ Programming Lecture 14 Arrays – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook slides)
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
Declaring fstream Objects An istream object named cin connects program and keyboard An ostream object named cout connects the program and the screen These.
Exploring the C++ Stream Library Copyright 2006 Oxford Consulting, Ltd1 February IO Streams  IOStreams are part of the Standard C++ library.
CS162 External Data Files 1 Today in CS162 External Files What is an external file? How do we save data in a file?
Lecture 14 Arguments, Classes and Files. Arguments.
Copyright © 2006 Pearson Addison-Wesley. All rights reserved Today’s Lecture  I/O Streams  Console I/O  File I/O  Tools for File I/O  Sequential.
File I/O in C++. Using Input/Output Files A computer file  is stored on a secondary storage device (e.g., disk);  is permanent;  can be used to provide.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
File I/O in C++ I. Using Input/Output Files A computer file is stored on a secondary storage device (e.g., disk); is permanent; can be used to provide.
Advanced File Operations Chapter File Operations File: a set of data stored on a computer, often on a disk drive Programs can read from, write to.
Introduction to Programming Lecture 12. Today’s Lecture Includes Strings ( character arrays ) Strings ( character arrays ) Algorithms using arrays Algorithms.
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Basic concepts of C++ Presented by Prof. Satyajit De
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 7 Text Input/Output Objectives
What is a File? A file is a collection on information, usually stored on a computer’s disk. Information can be saved to files and then later reused.
Copyright © 2003 Pearson Education, Inc.
Lecture 5A File processing Richard Gesick.
Today’s Lecture I/O Streams Tools for File I/O
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CS150 Introduction to Computer Science 1
CS150 Introduction to Computer Science 1
CHAPTER 4 File Processing.
Reading from and Writing to Files
Chapter 1 c++ structure C++ Input / Output
Reading from and Writing to Files
Presentation transcript:

1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04

firstlastnextprevious richard jones Files Files are used to store data longer term And for data that is too big to fit in main memory

firstlastnextprevious richard jones Data streams Data streams take data to and from our programs: chomp2d.exe player requests via keyboard cin >> row >> col board display via console cout << board[row]

firstlastnextprevious richard jones File streams Streams of data don’t have to use keyboard and console: chomp2d.exe to load a board position file >> board[] to save a board position file << board[] file

firstlastnextprevious richard jones Buffers The CPU operates quickly The file access is relatively slow Characters to and from the program are placed in a buffer:

firstlastnextprevious richard jones Buffers chomp2d.exe file The program i/o takes place via the buffer program running in CPU physical file on disc

firstlastnextprevious richard jones C++ streams The header file contains object similar to cin and cout that can be used with files. The stream object in a C++ program needs to be associated with a physical file.

firstlastnextprevious richard jones Syntax for creating a file #include //header file const char FNAME[9] = "text.txt"; //const string // declare a filestream object fstream file; // associate stream with physical file file.open(FNAME, ios::out); text.txt file program physical file filestream object program.exe

firstlastnextprevious richard jones Check if it went OK! if (file == NULL) // check for success { cout << "Could not open file " << FNAME; return 1; } else { // write a message to the file file << "Hello text file\nHow are you today?"; // close the file and empty the buffer file.close(); } return 0; }

firstlastnextprevious richard jones Program output None at the console, but in the file we find: Hello text file How are you today? Hello text file How are you today? text.txt

firstlastnextprevious richard jones Getting our text back char line1[SIZE], line2[SIZE]; // to read the lines into fstream file; // declare a filestream object file.open(FNAME, ios::in); // associate stream with physical file if (file == NULL) // check for success { cout << "Could not open file " << FNAME; return 1; } else { // read a message from the file file >> line1; file >> line2; file.close(); cout << line1 << endl << line2 << endl; }

firstlastnextprevious richard jones Output of the program Hello text Process in_file finished Oops, something got lost, file streams work much like cin, it stopped reading at whitespace file >> line1; replace with?

firstlastnextprevious richard jones Getting multiword lines // read a message from the file file.getline(line1, SIZE, '\n'); file.getline(line2, SIZE, '\n'); stops reading at the end of the line

firstlastnextprevious richard jones Summary Files are collections of data on disk Streams are used to transfer data between physical file and our programs. contains the fstream object – among other things.

firstlastnextprevious richard jones ios::in & ios::out These are mode indicators (Bronson chapter 14 p 615) mode indicatormeaning ios::inopen for input ios::outopen for output ios::appopen in append mode ios::truncdelete existing file contents ios::noreplacedon’t delete existing file

firstlastnextprevious richard jones Writing char arrays to file This is a useful technique if you have to write a board game in which you have to save/load a position. Which you do…

firstlastnextprevious richard jones Chomp – sequel to the return #********* ********** Please input the row and column to chomp (-1, -1 to quit): 2 3 #********* ********** ***

firstlastnextprevious richard jones Save is automatic on quit void save_file(char board[MAXROWS][MAXCOLS]) { fstream file; file.open(FNAME, ios::out); if (file != NULL) // success, write rows to the file plus end marker { for(int r = 0; r < (MAXROWS); r++) file << board[r] << endl; file.close(); } else { cout << "File save failed"; }

firstlastnextprevious richard jones Open is optional bool load_file(char board[MAXROWS][MAXCOLS]) { char yes_no; fstream file; cout << "\nWould you like to load a saved file (y/n)? "; cin >> yes_no;

firstlastnextprevious richard jones Options carried out if( (yes_no == 'y') || (yes_no == 'Y') ) { file.open(FNAME, ios::in); // open for input if( file !=NULL) { // check status // OK read in the board as an array of strings, // NB there are no spaces in a chomp board(!). for(int r = 0; r < (MAXROWS); r++) file >> board[r]; file.close(); return true; } else cout << "File load failed"; } return false;

firstlastnextprevious richard jones For a 2D char array After you have opened the file (and checked status) Write lines as rows, terminate with newline character ‘\n’ Read data as rows, use >> if data has no spaces. Use file.getline(line, SIZE, ‘\n’) otherwise.