C++ Programming Lecture 8 File Processing

Slides:



Advertisements
Similar presentations
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Advertisements

CPSC 231 D.H. C++ File Processing 1 Learning Objectives §C++ I/O streams. §Reading and writing sequential files. §Reading and writing random access files.
計算機概論實習 Files and Streams C++ views file as sequence of bytes Ends with end-of-file marker n-1 end-of-file marker 67 This is.
Copyright © 2012 Pearson Education, Inc. Chapter 12: Advanced File Operations.
1 Lecture 6: Input/Output (II) Introduction to Computer Science Spring 2006.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
C++ Programming: Program Design Including Data Structures, Fourth Edition Chapter 3: Input/Output.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 3: Input/Output.
Chapter 3: Input/Output
You gotta be cool. Stream Stream Output Stream Input Unformatted I/O with read, gcount and write Stream Manipulators Stream Format States Stream Error.
C++ Programming: Program Design Including Data Structures, Fifth Edition Chapter 3: Input/Output.
Lecture 6: Expressions and Interactivity (Part II) Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 3: Input/Output.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
I/O and Data Formatting Introduction to Class Concepts INFSY 307 Spring 2003 Lecture 3.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
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.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Chapter 3: Input/Output
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
1 CSC241: Object Oriented Programming Lecture No 32.
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.
CSCI 333 Data Structures I/O with C++ 20 October, 2003.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Chapter 3: Input/Output. Objectives In this chapter, you will: – Learn what a stream is and examine input and output streams – Explore how to read data.
Introduction Every program takes some data as input and generate processed data as out put . It is important to know how to provide the input data and.
Chapter 14: Sequential Access Files
File Processing.
File Processing.
Topic 2 Input/Output.
17 File Processing.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
CPS120: Introduction to Computer Science
CPS120: Introduction to Computer Science
TMF1414 Introduction to Programming
Output Stream Formatting
CPSC 231 D.H. C++ File Processing
Input and Output Chapter 3.
17 File Processing.
Standard Input/Output Streams
Standard Input/Output Streams
Input/Output Handouts: Quiz 2, Unit 3 practice sheets.
Chapter 11 – File Processing
17 File Processing.
Basic Input and Output C++ programs can read and write information using streams A simple input stream accepts typed data from a keyboard A simple output.
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
File Processing in C++ Data is stored in files so that it may be retrieved for processing when needed.
Chapter 3 Input output.
Formatting the Output The C++ standard library supplies many manipulators: endl, setw, fixed, showpoint, setprecesion. If we want to use endl, fixed, or.
Fundamental of Programming (C)
Formatted Input, Output & File Input, Output
CPS120: Introduction to Computer Science
CHAPTER 4 File Processing.
Reading from and Writing to Files
EPSII 59:006 Spring 2004.
C++ Programming Lecture 20 Strings
Reading from and Writing to Files
Input/Output Streams, Part 2
Presentation transcript:

C++ Programming Lecture 8 File Processing By Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department

The Hashemite University Outline Introduction. The Data Hierarchy. Files and Streams. Creating a Sequential Access File. Reading Data from a Sequential Access File. Updating Sequential Access Files. Input/Output Manipulations. Examples. The Hashemite University

The Hashemite University Introduction Data files can be created, updated, and processed by C++ programs Files are used for permanent storage of large amounts of data Storage of data in variables and arrays is only temporary The Hashemite University

The Hashemite University The Data Hierarchy I Bit - smallest data item value of 0 or 1 Byte – 8 bits used to store a character Decimal digits, letters, and special symbols Field - group of characters conveying meaning Example: your name Record – group of related fields Represented as a struct or a class Example: In a payroll system, a record for a particular employee that contained his/her identification number, name, address, etc. File – group of related records Example: payroll file Database – group of related files The Hashemite University

The Hashemite University The Data Hierarchy II 1 01001010 Judy Green Sally Black Tom Blue Iris Orange Randy Red File Record Field Byte (ASCII character J) Bit Record key identifies a record to facilitate the retrieval of specific records from a file Sequential file records typically sorted by key The Hashemite University

The Hashemite University Files and Streams C++ views each file as a sequence of bytes File ends with the end-of-file marker Stream is created when a file is opened File processing Headers <iostream> and <fstream> class ifstream - input class ofstream - output class fstream - either input or output The Hashemite University

Creating a Sequential Access File Files are opened by creating objects of stream classes ifstream, ofstream or fstream File stream member functions for object file: file.open(“Filename”, fileOpenMode); file.close(); closes file File open modes: Makes a "line of communication" with the object and the file. The Hashemite University

The Hashemite University Example I #include<iostream> #include<fstream> using namespace std; int main() { ofstream out_file("HU.txt", ios::out); //or HU.dat if(out_file == 0) cout <<"Unable to open the file\n"; exit(1); } for(int i = 0; i < 9; i++) out_file << i << endl; out_file.close(); return 0; The Hashemite University

Example II – Storing Data Entered By the User #include<iostream> #include<fstream> using namespace std; int main() { ofstream out_file("HU.txt", ios::in); //or HU.dat int a; if(out_file == 0) cout <<"Unable to open the file\n"; exit(1); } cout <<"Enter an integer to save on file\n"; cin >> a; while(a != -1) //end when -1 is entered { out_file << a << endl; cout <<"Enter an integer to save on file\n"; cin >> a; } out_file.close(); return 0; The Hashemite University

The Hashemite University Notes You will find the created text file in the same directory in which you save the source code unless you specify the full path of where you want to save the created text file. The extension of the created file could be .txt or .dat Pay attention to how the data is written on the file (wether there are formatted, only one value is stored per line or multiple ones, etc.) to read the data correctly. All attributes associated with cin object (stopping at enetr or white spaces, read vales based on the data type of the holder, etc.) are applied to files reading. ofstream are opened for output by default.so, in the previous example writing ofstream out_file("HU.txt"); is the same as ofstream out_file("HU.txt", ios::out); The Hashemite University

Reading Data from a Sequential Access File File stream member functions for repositioning file position pointer: seekg (seek get) for istream and seekp (seek put) for ostream // position to the nth byte of fileObject // assumes ios::beg fileObject.seekg( n ); // position n bytes forward in fileObject fileObject.seekg( n, ios::cur ); // position y bytes back from end of fileObject fileObject.seekg( y, ios::end ); // position at end of fileObject fileObject.seekg( 0, ios::end ); tellg and tellp – return current location of pointer location = fileObject.tellg() //returns long int The Hashemite University

The Hashemite University Example #include<iostream> #include<fstream> using namespace std; int main() { ifstream in_file("HU.txt", ios::in); //or HU.dat int a; if(in_file == 0) cout <<"Unable to open the file\n"; exit(1); } for(int i = 0; i < 9; i++) in_file >> a; cout << a << endl; in_file.close(); return 0; The Hashemite University

The Hashemite University Example Output The Hashemite University

The Hashemite University Note The file that you read from must be stored in the same directory in which you store the source code file, otherwise you must specify the full path of where you store the file. The extension of the created file could be .txt or .dat ifstream are opened for input by default. so, in the previous example writing ifstream in_file("HU.txt"); is the same as ifstream in_file("HU.txt", ios::in); The Hashemite University

Updating Sequential Access Files Cannot be modified without the risk of destroying other data Formatted text which is output to a file is very different than the internal representation 300 White 0.00 400 Jones 32.87 (old data in file) if we want to change White's name to Worthington, 300 White 0.00 400 Jones 32.87 300 Worthington 0.00ones 32.87 300 Worthington 0.00 Data gets overwritten The Hashemite University

Input/Output Manipulations C++ provides various stream manipulators that perform formatting tasks. Till now we have dealt with screen output using cout. So, all these manipulators will format the text that appears on the screen. Examples: Setting the precision of the displayed floating point numbers. Setting the field width. Showing trailing zeros and decimal point in floating point numbers. Filling field with specific characters. The Hashemite University

Floating-Point Precision (setprecision) sets number of digits to the right of decimal point (with rounding) parameterized stream manipulator Like all parameterized stream manipulators, <iomanip> required specify precision: cout << setprecision(2) << x; Changes last until a different value is set. The Hashemite University

The Hashemite University Field Width(setw) setw stream manipulator sets field width (number of character positions a value should be output or number of characters that should be input). returns to previous width after the implementation (so it is applied to the next insertion or extraction only). If values processed are smaller than width, fill characters inserted as padding. Large values are not truncated - full number printed. Displayed output is right justified. Examples: cin >> setw(5) >> number; cout << setw(5) << number; The Hashemite University

Trailing Zeros and Decimal Points (showpoint) ios::showpoint forces a float with an integer value to be printed with its decimal point and trailing zeros cout << showpoint << 79.0; will print 79.00000 number of zeros determined by precision settings Applied for all subsequent output operations. To reset this option apply the following command: cout << noshowpoint; Remember trailing zeros are applied to floating point numbers only and not for integers. The Hashemite University

The Hashemite University Padding (setfill) If you specify the field width to 10 and you print 56 then this number will be preceded by white spaces. White space here is the fill character. You can change this fill character to any other character you want. Applied for all subsequent cout statements. To remove it set the fill character to white space to return to the default settings. setfill manipulator sets fill character cout << setfill ('*'); cout << setw(5) << 3; //output is ****3 The Hashemite University

Floating-Point Numbers; Scientific Notation (scientific, fixed) forces output of a floating point number in scientific notation: 1.946000e+009 Only one digit in the integer part of the floating point number and n digits after the floating point (depends on the precision value) and put the suitable power after the e exponent. E.g.: cout << scientific << 234.5678; fixed forces floating point numbers to display a specific number of digits to the right of the decimal (specified with precision) and the whole integral part. E.g.: cout << fixed << 234.5e-1; Applied for all subsequent cout statements. The default setting in C++ is the fixed formatting or depends on the original value of the floating point number. The Hashemite University

The Hashemite University Additional Notes This lecture covers the following material from the textbook: Chapter 14: Sections 14.1 – 14.6 Chapter 12: Sub-Sections 12.6.2, 12.6.3, 12.7.1, 12.7.3, 12.7.5 This is the last lecture included in the first exam material. The Hashemite University