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)

Slides:



Advertisements
Similar presentations
File Management in C. A file is a collection of related data that a computers treats as a single unit. File is a collection of data stored permanently.
Advertisements

Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
File I/O Finished off Colin Cherry standing in for Dr. Lin.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 13 Advanced.
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.
Chapter 8: I/O Streams and Data Files. In this chapter, you will learn about: – I/O file stream objects and functions – Reading and writing character-based.
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.
CSE Lectures 22 – Huffman codes
COIT29222-Structured Programming Lecture Week 12  Reading: Textbook (4 th Ed.), Chapter 14 Textbook (6 th Ed.), Chapter 17 Study Guide Book 3, Module.
Darbas su failais Arnas Terekas IT 1gr. Vilniaus universitetas Matematikos ir informatikos fakultetas.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
C++ for Engineers and Scientists Second Edition Chapter 8 I/O File Streams and Data Files.
FILE HANDLING IN C++.
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.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 12: Advanced File Operations.
Programming Principles II Lecture Notes 7 Files Andreas Savva.
STL List // constructing lists #include int main () { // constructors used in the same order as described above: std::list first; // empty list of ints.
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.
“After a day spent staring at a computer monitor, think of a book as a kind of screen saver for your brain” “One good reason why computers can do more.
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.
I/O in C++ October 7, Junaed Sattar. Stream I/O a stream is a flow of bytes/characters/ints or any type of data input streams: to the program output.
C++ FILE I/O.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 13 File Input and.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 12 Advanced File Operations.
DCT1063 Programming 2 CHAPTER 3 FILE INPUT AND FILE OUTPUT Mohd Nazri Bin Ibrahim Faculty of Computer, Media & Technology TATi University College
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Member Functions for Reading and Writing Files 12.5.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Streams, and File I/O Review. STREAMS Review STREAMS Streams are sequences of bytes. C++ I/0 occurs in streams Input – bytes flow from device to memory.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
Fall 2015CISC/CMPE320 - Prof. McLeod1 CISC/CMPE320 Assignment 1 due Friday, 7pm. RAD due next Friday. Presentations week 6. Today: –More details on functions,
1 CSC241: Object Oriented Programming Lecture No 32.
 Random access vs. sequential access  Current position pointer (CP)  Record numbers  seekg command  tellg command  Program using random access.
File Processing Files are used for data persistance-permanent retention of large amounts of data. Computer store files on secondary storage devices,such.
C++ Programming: chapter 6 – iostream 2015, Spring Pusan National University Ki-Joune Li 1.
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.
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.
Starting Out with C++ 1 Chapter 12: File Operations What is a File? A file is a collection on information, usually stored on some electronic medium. Information.
File Handling. Read data from file and display it on screen #include int main() { /* fin object is created with parameterzied constructor */ ifstream.
Ms N Nashandi Dr SH Nggada 2016/01/03Ms N Nashandi and Dr SH Nggada1 Week 6 -File input and output in C++
Programming II I/O Streams and Data Files 1(c) Asma AlOsaimi.
CS212: Object Oriented Analysis and Design
Programming with ANSI C ++
17 File Processing.
Chapter 13: Advanced File and I/O Operations
C ++ MULTIPLE CHOICE QUESTION
CPSC 231 D.H. C++ File Processing
FILE HANDLING IN C++.
17 File Processing.
17 File Processing.
آشنایی با ساختارها و کار با فایلها
files Dr. Bhargavi Goswami Department of Computer Science
Chapter 13: Advanced File and I/O Operations
C++ FileIO pepper.
Topics Input and Output Streams More Detailed Error Testing
Chapter 12: Advanced File Operations.
Data File Handling in C++
C++ Programming: chapter 6 – iostream
Data File Handling RITIKA SHARMA.
C++ for Engineers and Scientists Second Edition
Presentation transcript:

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) Binary files: A way to store data in a secondary storage device using the same binary representation as main memory.

Binary vs. Text ASCII Table

Data Records Generally, when reading binary files, we read (or write) records. A file pointer is used to track the current position in the file. The current position is measured as a byte offset from the beginning of the file.

Random Access Random access files allow the manipulation of the file pointer to read & write data from different locations within the file. The next read or write will take place starting at the current location of the file pointer (fp) fp

Random Access Text files only allow sequential access Data must be read in the order it is stored, starting at the beginning of the file. Binary files allow sequential & random access.

Sequential Access When the current position of a binary file pointer is the beginning of a file, a read (or write) will advance the file pointer to the next record. fp

Random Access However, the file pointer can also be placed randomly within the file, allowing access to specific records at any place in the file (similar to using arrays). fp

fstream With text files, we have declared streams as an ifstream or ofstream. With binary files is possible to declare simply an fstream and then open files for input, output, or both… fstream instr, outstr, inoutstr; instr.open(“infile.txt”, ios::in); outstr.open(“outfile.txt”, ios::out); inoutstr.open(“file.txt”, ios::in | ios::out); Bitwise OR

The Mode Parameter ios::in This indicates that the stream will be used for input. ios::out This indicates that the stream will be used for output. ios::binary This causes the file to be accessed as a binary file. If you need to specify more than one of them simply place a vertical bar between them.

file.clear() It is possible to read a file over and over (you will do this in program 5). You should be aware that there is a problem if during any read, you reach the end-of-file marker. This sets a flag that remains set the next time you try to read the file again. To clear the eof flag, we use if(file.eof( )) file.clear( );

FYI This is how you can reuse (i/o)fstreams… ifstream fin; fin.open("first.txt");... fin.close(); fin.clear(); fin.open("second.txt");...

Placing the file pointer The file pointer moves throughout the file in offsets (the size of a single record). Various arguments and fstream functions are associated with the file pointer: sizeof() fstr.seekg() fstr.tellg() ios::beg ios::end ios::cur

. seekg() Moves the file pointer to another location within the file. ios::beg location (in bytes) from the beginning of the file. ios::cur location (in bytes) from the current file pointer position. ios::end location (in bytes) from the end of the file.

sizeof( ); When moving through binary files, you must move in record size leaps. sizeof( ) is a function to return the size of an argument (parameter). sizeof(int) will return the number of bytes necessary to store an integer (4 on our compiler). sizeof(myObject) will return the byte size of the declared object.

.seekg( ) –.seekp( ).seekp( ) can only be used with single directional streams. Use.seekg( ) for files opened for both input and output. These functions are used to move the file pointer within the file. Two parameters are required 1) the number of bytes to move 2) the position to start measuring from

fstream fstr; fstr.seekg(); To place the file pointer at the beginning of the file we could use: fstr.seekg(0, ios::beg); To move the file pointer to the end of the file use: fstr.seekg(0, ios::end);

int main() { int x; fstream infile; infile.open("file.dat", ios::bin | ios::in); infile.seekg(9, ios::beg); // move 9 bytes from the beginning of the file … infile.seekg(-10, ios::cur); //back up 10 bytes from current position … infile.seekg(0,ios::end); //move to the end of the file infile.close(); }

To advance the pointer n records from the beginning of the file use: fstr.seekg(n * sizeof(record), ios::beg); fp fstr.seekg(3 * sizeof(record), ios::beg); fstr.seekg(3 * 30, ios::beg); fstr.seekg(90, ios::beg); 30 bytes 0 29|30 59|60 89|90

To move back one record from the current location use: fstr.seekg(-sizeof(record), ios::cur); fp bytes 0 29|30 59|60 89|90 fp

Practice How would you place the pointer… n records from the end of a file fstr.seekg(-n * sizeof(record), ios::end); n records past the current position fstr.seekg(n * sizeof(record), ios::cur); pointing to the last record in a file fstr.seekg(-sizeof(record), ios::end);

.tellg( ); This function will return the current byte location of the pointer. The number of bytes from the beginning of the file. You can use this to find the number of records in a file using: fstr.seekg(0, ios::end); numRec = fstr.tellg()/sizeof(record);

I/O with Binary Files First you must open an fstream It must be opened for both input and output AND binary mode fstream inOut; inOut.open("myFile.bin", ios::binary | ios::in | ios::out); if(!inOut.is_open( )) // same as inOut.fail( ) { cout << "error opening myFile.bin" << endl; exit(1); }

read( ) – write ( ) read( ) and write( ) are fstream functions that perform I/O with a byte stream. Each takes the address of a buffer and the number of bytes to transfer (count).

I/O Buffers The buffer is an array of characters that stores the data as it is sent or received from a device. Non-character data types require a type conversion, using the (char*) type cast. This tells the compiler to consider the buffer as an array of characters (regardless of its contents).

Example fstream fstr; int intData; int arr[20]; // array of 20 integers fstr.read((char*) &intData, sizeof(int)); fstr.write((char*) arr, sizeof(arr));

program 5 In program 5 you will not be reading the entire file into your program. You will read a single record at a time, use it, update it, or delete it. You will use the index.txt file to determine where records are in the binary file. The Index class will help you do this

Update a Player 1. Find the player’s name in the index file and thus the location index associated with that player. 2. Use the index to move the file pointer to the desired record. 3. Read the Player record and update the data. 4. Move back one record in the file.(Why?) 5. Write the updated record to the file.

More Command-Line Arguments

c:\>a.out PlayerRecord.bin index.txt int main(int argc, char *argv[]) argc = 3 argv[0] = a.out argv[1] = PlayerRecord.bin argv[2] = index.txt

Opening Files to open the first file for input and the second file for output we could use ifstream first; inInv.open(argv[i]); ofstream second; outInv.open(argv[2]); or fstream first, second; first.open(argv[1], ios::in); second.open(argv[2], ios::out);