Chapter 8 Data File Basics.

Slides:



Advertisements
Similar presentations
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.
Advertisements

File Input/Output. External Files Batch –Requires use of data files (save to disk) –Batch can be run during off peak use –allows things to be complete.
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.
1 Text File I/O Chapter 6 Pages File I/O in an Object-Oriented Language Compare to File I/O in C. Instantiate an ofstream object. Like opening.
File streams Chapter , ,
CPS120: Introduction to Computer Science Lecture 15 B Data Files.
1 Programming with Data Files Chapter 4 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
CS-212 C++ I/O Dick Steflik. C++ I/O Modeled after UNIX’s concept of a “stream” –conceptionally a stream is a continuous flow of characters/bytes from.
Chapter 5: Loops and Files.
An Introduction to Programming with C++ Fifth Edition Chapter 13 Sequential Access Files.
1 Programming with Data Files Chapter 4. 2 Standard Input Output C++ Program Keyboard input cin Output Screen cout.
Chapter 15.
Programming with Data Files Chapter 4. Standard Input Output C++ Program Keyboard input cin Output Screen cout.
CS 1 Lesson 5 Loops and Files CS 1 -- John Cole.
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.
Chapter 3 Interactivity and Expressions CSC 125 Introduction to C++ programming.
STREAMS AND FILES OVERVIEW.  Many programs are "data processing" applications  Read the input data  Perform sequence of operations on this data  Write.
CSIS 123A Lecture 8 Streams & File IO Glenn Stevenson CSIS 113A MSJC.
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.
Topics 1.File Basics 2.Output Formatting 3.Passing File Stream Objects to Functions 4.More Detailed Error Testing 5.Member Functions for Reading and 6.Writing.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
1 firstlastnextprevioushome richard jones COMP103A Introduction to Computer Science 1 Richard Jones G2.04
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.
Computer Programming TCP1224 Chapter 13 Sequential File Access.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Disk Files for I/O your variable (of type ifstream) your variable (of type ofstream) disk file “myInfile.dat” disk file “myOut.dat” executing program input.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Loops and Files. 5.1 The Increment and Decrement Operators.
Input/Output CSci 588: Data Structures, Algorithms and Software Design Fall 2011 All material not from online sources copyright © Travis Desell, 2011
Streams One of the themes of this course is that everything can be reduced to simple (and similiar) concepts. Streams are one example. Keyboard and Screen.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 9. Streams & Files.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
Learners Support Publications Working with Files.
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.
CPS120 Introduction to Computer Science Exam Review Lecture 18.
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.
Writing to Files and Reading From Files. Writing Data to a File Creating a new file, a.dat or.txt file #include #include // for file writing #include.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students File Input and Output Checking input for errors.
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.
1 Stream Input and Output Read Text, page Keyboard and Screen I/O #include cin (of type istream) cout (of type ostream) KeyboardScreen executing.
Computer Programming II Lecture 9. Files Processing - We have been using the iostream standard library, which provides cin and cout methods for reading.
Chapter 4 Strings and Screen I/O. Objectives Define strings and literals. Explain classes and objects. Use the string class to store strings. Perform.
Chapter 14: Sequential Access Files
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.
Input and Output Chapter 3.
FILE HANDLING IN C++.
Interactive I/O Input from keyboard Must prompt user User friendly
files Dr. Bhargavi Goswami Department of Computer Science
Today’s Lecture I/O Streams Tools for File I/O
Text Files All the programs you've seen so far have one thing in common: Any data the program uses or calculates is lost when the program ends. In order.
Chapter 12: Advanced File Operations.
CS150 Introduction to Computer Science 1
File I/O.
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
Programming with Data Files
Reading from and Writing to Files
Presentation transcript:

Chapter 8 Data File Basics

Objectives Understand concepts of sequential-access and random-access files. Open and close sequential files. Write data to sequential files. Read data from sequential files. Use other sequential file techniques.

Why Use Data Files? Recall that RAM holds data only as long as the computer is on. Data in RAM is lost as soon as your program ends. Data must be saved to a file to prevent data loss. There is more hard drive space available than RAM space.

Sequential-Access A sequential-access file works like an audiocassette tape. To retrieve specific data from a sequential-access data file, you must start at the beginning of the file and search for the data or records you want while moving through the file. Sequential-access files are the most widely used data files.

Random-Access Files A random-access file works like an audio compact disc (CD). A random-access data file allows to you to move directly to any data in the file. Random-access files are most often used to store databases. Random-access files often occupy more disk space than sequential-access files.

Declaring File Streams Getting data to and from files involves streams called file streams. When declaring a file stream, you select a stream type and a name for the stream. Use ofstream outfile; for writing. Use ifstream infile; for reading.

Opening a File When you open a file, a physical disk file is associated with the file stream you declared. You must provide the name of the file you want to open, and whether you need to read or write data. After the filename, you need to specify the stream operation mode. Use ios::out when creating an output file and ios::in when opening a file for input.

Open File Examples Example 1: writing to a file ofstream high_scores; //stream for writing high_scores.open("SCORES.DAT", ios::out); //Create the output file. Example 2: reading from a file ifstream infile; // stream for reading infile.open("MYDATA.DAT", ios::in); // Open the file for input.

Closing a File After a file has been opened, you can begin writing to it or reading from it. When you complete your work with the file, you must close it. infile.close(); // Close the input file.

Writing Data to Files Writing data to a sequential-access data file employs the insertion operator (<<) that you use when printing data to the screen. Instead of using the output operator to direct data to the standard output device (cout), you direct the data to the file stream.

Reading Data From Files Before you can read data from a file you must open the file for input using a file stream. When you open a file using ios::in, the file must already exist. Some compilers will create an empty file and give unpredictable results.

Reading Numeric Data When reading strictly numeric data, you can use the extraction operator (>>) as if you were getting input from the keyboard. Instead of using cin as the input stream, use your file stream name.

Adding Data to the End of a File Adding data to the end of an existing file is called appending. To append data to an existing file, open the file using the ios::app stream operation mode. If the file you open for appending does not exist, the operating system creates one for you.

Detecting the End of a File When reading strings from a file, the getline method returns a value of true as long as it finds valid data to read. If the end of the file is reached, the getline method will return a value of false. When reading numeric values, you must detect whether the operation has failed.

Detecting the End of a File A loop like the following can be used to find the end of a file that contains strings. while(getline(infile,instring)) { cout << instring << endl; }

Detecting the End of a File A loop like the following can be used to find the end of a file that contains numeric data. do { infile >> x; if(!infile.fail()) { cout << x << endl; } } while(!infile.fail());

Using Multiple Files Reading and writing to multiple files at the same time is possible. Simply declare a separate filestream for each file that needs to be manipulated. In this way, you could even add data to the middle of an exiting file.

Summary Data files allow for the storage of data prior to a program’s ending and the computer’s being turned off. In a sequential-access file, data must be written to and read from the file sequentially. In a random-access file, any record can be accessed directly.

Summary The first step to using a file is declaring a file stream. After a file stream has been declared, the next step is to open the file. After data is written or read, the file must be closed. The insertion operator (<<) is used to write to a data file.

Summary When reading numeric data, use the extraction operator. When reading string data or when reading from a file with both string and numeric data, read the data into string objects. Adding data to the end of an existing file is called appending.

Summary The getline method detects the end of the file when reading strings from a file. The fail function detects the end of a file when reading numbers. You can use more than one file at a time by declaring multiple file streams.