File Processing.

Slides:



Advertisements
Similar presentations
1 Classes and Data Abstraction and File Processing Lecture 13.
Advertisements

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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
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.
 2006 Pearson Education, Inc. All rights reserved File Processing.
計算機概論實習 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.
 2006 Pearson Education, Inc. All rights reserved File Processing.
17 File Processing. OBJECTIVES In this chapter you will learn:  To create, read, write and update files.  Sequential file processing.  Random-access.
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
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.
Streams, Files. 2 Stream Stream is a sequence of bytes Input stream In input operations, the bytes are transferred from a device to the main memory Output.
1 Advanced Input and Output COSC1567 C++ Programming Lecture 9.
C++ How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
FILE HANDLING IN C++.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
File handling in C++ BCA Sem III K.I.R.A.S. Using Input/Output Files Files in C++ are interpreted as a sequence of bytes stored on some storage media.
 2003 Prentice Hall, Inc. All rights reserved. 1 IS 0020 Program Design and Software Tools Stack/Queue - File Processing Lecture 10 March 29, 2005.
 2008 Pearson Education, Inc. All rights reserved File Processing.
An Introduction to Programming with C++ Sixth Edition Chapter 14 Sequential Access Files.
計算機程式語言 Lecture 17-1 國立台灣大學生物機電系 林達德 17 File Processing.
File I/O. fstream files File: similar to vector of elements Used for input and output Elements of file can be –character (text)struct –object (non-text.
C++ FILE I/O.
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
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 Introduction Introduce some standard library functions.
© 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
1 CSC241: Object Oriented Programming Lecture No 32.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
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.
Lecture 14 Arguments, Classes and Files. Arguments.
FILE HANDLING(WORKING WITH FILES) FILE- A file is a collection of related data stored in a particular area on the disk. STREAMS- interface between the.
Binary Files. Text Files vs. Binary Files Text files: A way to store data in a secondary storage device using Text representation (e.g., ASCII characters)
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.
Data File Handling in C++ Data File Handling in C++ Paritosh Srivastava PGT (Comp. Science) Kendriya VidyalayaJoshimath.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
نظام المحاضرات الالكترونينظام المحاضرات الالكتروني I/O and File management(cont.) Binary and random files.
2008YeungNam Univ. SE Lab. 1 C++ views each file as a sequence of bytes terminated by EOF-marker. Header files Files are opened by creating objects of.
CS212: Object Oriented Analysis and Design
Chapter 14: Sequential Access Files
Programming with ANSI C ++
17 File Processing.
Tutorial4us.com File Handling in C++ Tutorial4us.com.
TMF1414 Introduction to Programming
CISC/CMPE320 - Prof. McLeod
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
Chapter 11 – File Processing
Advanced Input and Output
MSIS 670: Object-Oriented Software Engineering
17 File Processing.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
Tutorial4us.com. File A file is a collection of related data stored in a particular area on the disk. The data is stored in disk using the concept of.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Fundamental of Programming (C)
CS150 Introduction to Computer Science 1
Reading from and Writing to Files
EPSII 59:006 Spring 2004.
C++ Programming Lecture 8 File Processing
Reading from and Writing to Files
Presentation transcript:

File Processing

Introduction Files are used for permanent retention of large amount of data Storage of data in variables is temporary Files are stored on secondary storage devices Magnetic disks, optical disks, tapes

Data Hierarchy Bit: manipulated by computer circuitry binary digit: 0 or 1, Byte: commonly composed of 8 bits Decimal digits, letters, special symbols Field: a group of bytes that conveys meaning Record: composed of related fields File: a group of related records Database: a group of related files

Files and Streams C++ views files as a sequence of bytes No concept of record When a file is opened, an object is created and a stream is associated with it To process file in C++, <iostream>,<fstream> must be included. ios istream iostream ostream ofstream ifstream fstream

Creating a Sequential File – Maintain Bank Balances #include<iostream> #include<fstream> void main() { ofstream clientF(“clients.dat”, ios::out); if (!clientF){ //overloaded ios operator ! cerr<<“File could not be opened”<<endl; exit(1); } int account; char name[30]; double balance; while (cin>>account>>name>>balance){ clientF<<account<<name<<balance<<‘\n’; Files are opened by creating objects of ifstream, ofstream, or fstream Explicitly close file: clientF.close();

Reading From a Sequential File #include<iostream> #include<fstream> #include<iomanip> void main() { ifstream clientF(“clients.dat”, ios::in); if (!clientF) … int account; char name[30]; double balance; while (clientF>>account>>name>>balance){ cout<<setiosflags(ios::left)<<setw(10) <<account<<setw(13)<<name<<setw(7) <<setprecision(2) <<resetiosflags(ios::left) <<balance<<‘\n’; }

Reposition File Position Pointer Both istream and ostream provide member function for repositioning seekg(), seekp() istream fileObj(“abc.dat”); //position to the nth byte of fileObj fileObj.seekg(n, ios::beg); //position n bytes forward from current position fileObj.seekg(n, ios::cur); //position n bytes back from end of fileObj fileObj.seekg(n, ios::end); //get current position long loc = fileObj.tellg();

Updating Sequential Files There is no easy way! 300 White 0.00  300 Worthington 0.00 400 James 0.20 If record were rewritten beginning at the same location 300 Worthingto n 0.00mes 0.20 Inefficient / Awkward method Copy all records before White to new file nf Append Worthington record to nf Append all records after White to nf

Random-Access Files Sequential access files are inappropriate for instant-access applications Airline reservation systems, banking systems Require individual records to be accessed directly without searching through others C++ does not impose structure on file Application must create random-access files

Create Random Access Files Require records have same fixed length Program can calculate the exact location of any record relative to the beginning of files << should not be used int number; //number is a 4-byte integer outFile << number; //print 1 to 11 digits, //each digit requires 1 byte Use write() instead outFile.write(reinterpret_cast<const char*>(&number), sizeof(number));//always write 4 bytes

Create Random Access Files Require records have same fixed length Struct clientD { int acct; char la[15]; char fi[10]; double ba; }; void main(){ ofstream outF(“credit.dat”, ios::binary); if (!outF) … clientD client = {0, “”,””,0.0}; for (int i=0; i<100; i++) outF.write(reinterpret_cast<const char*>(&client), sizeof(client)); }

Write to Random Access Files Records have fixed length sizeof(client) void main(){ ofstream outF(“credit.dat”, ios::binary); if (!outF) … clientD client; cin >> client.acct; //1..100 while (client.acct>0 && client.acct<=100) { cin>>client.fi>>client.la>>client.ba; //Put file position pointer for object outF //to the right byte location outF.seekp((client.acct-1)*sizeof(client)); outF.write(reinterpret_cast<const char*>(&client), sizeof(client)); cin>>client.acct; }

Read from Random Access Files Records have fixed length sizeof(client) void main(){ ifstream inF(“credit.dat”, ios::in); if (!inF) … clientD client; inF.read(reinterpret_cast<char*>(&client), sizeof(client)); while (inF && !inF.eof()) { if (client.acct != 0) outputLine(cout, client); }

Read from Random Access Files void outputLine(ostream &output, const clientD &c){ output << setiosflags(ios::left) << setw(10) << c.acct << setw(16) << c.la << setw(11) << c.fi << setw(10) << setprecision(2)<<resetiosflags(ios::left) << c.ba <<endl; }

Summary C++ imposes no structure on a file. It views each file as a sequential stream of bytes. Files are opened by instantiating objects of stream classes ifstream, ofstream, and fstream. Streams provide communication channels between files and programs. A convenient way to implement random-access files is by using only fixed-length records.