Introduction to Computer Programming Lecture 18 Binary Files

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

CSCI 171 Presentation 12 Files. Working with files File Streams – sequence of data that is connected with a specific file –Text Stream – Made up of lines.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
CS1061: C Programming Lecture 19: Random Access Files A. O’Riordan, 2004, 2007 updated.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
1 Structure part 1 & File Processing. 2 Structures.
CP104 Introduction to Programming File I/O Lecture 33 __ 1 File Input/Output Text file and binary files File Input/output File input / output functions.
1 CHAPTER 7. Objectives: You’ll learn about;  Introduction  Fundamentals of binary data files  Processing binary files  Files with mixed type data.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
22. FILE INPUT/OUTPUT. File Pointers and Streams Declarations of functions that perform file I/O appear in. Each function requires a file pointer as a.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
File Handling In C By - AJAY SHARMA. We will learn about- FFile/Stream TText vs Binary Files FFILE Structure DDisk I/O function OOperations.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Characters and Strings File Processing Exercise C Programming:Part 3.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Chapter 8 : Binary Data Files1 Binary Data Files CHAPTER 8.
ECE 103 Engineering Programming Chapter 44 File I/O Herbert G. Mayer, PSU CS Status 6/4/2014 Initial content copied verbatim from ECE 103 material developed.
1 Lecture09: File I/O 11/19/2012 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
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.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C Programming Lecture 12 : File Processing
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
File Processing Part 2. Random Access File In sequential access file, record in a file created with the formatted output function fprintf are not necessarily.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
FILES IN C. File Operations  Creation of a new file  Opening an existing file  Reading from a file  Writing to a file  Moving to a specific location.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
1 Computer Programming Lecture 15 Text File I/O Assist. Prof Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 11 – File Processing
11 C File Processing.
File Processing Part 2.
Chapter 4 File Processing
Lecture 11 File input/output
TMF1414 Introduction to Programming
EKT120: Computer Programming
No Objects, No Type Safety
File Processing (Cont.) and Structures
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
Computer Programming Lecture 15 Text File I/O
What you need for the 1st phase of project
Introduction to Programming
Chapter 11 – File Processing
Lecture 15 Files.
Text and Binary File Processing
Fundamental of Programming (C)
Pertemuan 14 File Processing II
Chapter 12: File I/O.
EPSII 59:006 Spring 2004.
EPSII 59:006 Spring 2004.
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Introduction to Computer Programming Lecture 18 Binary Files Assist.Prof.Dr. Nükhet ÖZBEK Ege University Department of Electrical&Electronics Engineering nukhet.ozbek@ege.edu.tr

RECALL: Text File Handling in C ITCS 102 Computer Programming RECALL: Text File Handling in C Step 0: Include stdio.h Step 1: Declare a file handler (a.k.a. file pointer) as FILE * for each file Step 2: Open file using fopen() Step 3: Check if file is opened successfully Step 4a: Use fscanf() for input Step 4b: Use fprintf() for output Step 5: Close file using fclose() Dr. Bekir KARLIK

Binary Files When text files are used for storage of data, a program must spend extra effort to convert the stream of characters from an input file into integers and vice versa for output files If the file is not to be read by a human, we can avoid this unnecessary translation

Binary Files A binary file is a file created by executing a program that stores directly in the file the computer’s internal representation of each file component

Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

Binary file declaration Binary File Example Binary file declaration is the same as text file declaration FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

‘b’ character is used to Binary File Example ‘b’ character is used to denote that the file is a binary file FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp);

fclose function is used Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp); fclose function is used just as for text files

a different stdio library Binary File Example FILE *binaryp; int i; binaryp = fopen(“nums.bin”, “wb”); for (i = 2; i <= 500; i+=2) fwrite(&i, sizeof(int), 1, binaryp); fclose(binaryp); a different stdio library function is used

contents are to be copied fwrite function fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Address of the first memory cell whose contents are to be copied to the file

fwrite function fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Number of bytes to copy to the file for one component

sizeof operator sizeof operator can be applied to any data type to find the number of bytes used for storage of the data type sizeof operator can be applied to both built-in and user-defined types E.g. printf(“An integer requires %d bytes”,sizeof(int));

Number of values to write fwrite function fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Number of values to write to the file

Pointer to the file which fwrite function fwrite function has 4 input parameters fwrite(&i, sizeof(int), 1, binaryp); Pointer to the file which is opened for writing before

Notes on fwrite It is possible to write the contents of an entire array using just one fwrite call E.g., int score[10]; … fwrite(score, sizeof(int), 10, binaryp);

fread function fread function also requires four parameters Address of the first memory cell to fill Size of one value Maximum number of elements to copy from the file into memory File pointer to a binary file opened in mode “rb” fread function returns an integer indicating how many elements it copied from the file

Notes It is important not to mix file types A binary file created or written using fwrite must be read using fread A text file created or written using fprintf must be read using fscanf

Binary File Example /* FREAD.C: This program opens a file named FREAD.OUT and writes 25 characters to the file. It then tries to open the file and read in 25 characters. If the attempt succeeds, the program displays the number of actual items read. */ #include <stdio.h> int main( void ) { FILE *stream; int list[25]; int i, numread, numwritten; /* Open file in binary mode: */ stream = fopen( "fread.out", "wb" ); if( stream == NULL ) { printf( "Problem opening the file\n" ); return 1; }

Binary File Example (cntd) for ( i = 0; i < 25; i++ ) list[i] = i; /* Write 25 characters to stream */ numwritten = fwrite( list, sizeof( int ), 25, stream ); printf( "Wrote %d items\n", numwritten ); fclose( stream ); stream = fopen( "fread.out", "rb" ); if( stream == NULL ) { printf( "File could not be opened\n" ); return 2; }

Binary File Example (cntd) /* Attempt to read in 25 characters */ numread = fread( list, sizeof( int ), 25, stream ); printf( "Number of items read = %d\n", numread ); fclose( stream ); return 0; }

Structures and Binary Files Structures can also be written to and read from binary files using fwrite and fread functions Structures are written or read as a “package” with one function Structure arrays can also be written with one function

Data I/O Using Text and Binary Files Suppose we have a structure like the following: typedef struct { char name[10]; double diameter; int moons; … } planet_t;

Data I/O Using Text and Binary Files (Opening for reading) Text File I/O FILE *fp; fp = fopen(“planets.txt”,”r”); Binary File I/O fp = fopen(“planets.bin”,”rb”);

Data I/O Using Text and Binary Files (Opening for writing) Text File I/O FILE *fp; fp = fopen(“planets.txt”,”w”); Binary File I/O fp = fopen(“planets.bin”,”wb”);

Data I/O Using Text and Binary Files (Read one structure from file) Suppose: planet_t p; Text File I/O fscanf(fp,”%s %lf %d”, p.name,&(p.diameter),&(p.moons)); Binary File I/O fread(&p,sizeof(planet_t), 1, fp);

Data I/O Using Text and Binary Files (Read N structure from file) Suppose: planet_t p[N]; Text File I/O for(i=0; i < N; i++) fscanf(fp,”%s %lf %d”, p[i].name,&(p[i].diameter),&(p[i].moons); Binary File I/O fread(p,sizeof(planet_t), N, fp);

Data I/O Using Text and Binary Files (Write one structure to file) Suppose: planet_t p; Text File I/O fprintf(fp,”%s %lf %d”, p.name,p.diameter,p.moons); Binary File I/O fwrite(&p,sizeof(planet_t), 1, fp);

Data I/O Using Text and Binary Files (Write N structure from file) Suppose: planet_t p[N]; Text File I/O for(i=0; i < N; i++) fprintf(fp,”%s %lf %d”, p[i].name, p[i].diameter, p[i].moons); Binary File I/O fwrite(p,sizeof(planet_t), N, fp);

Data I/O Using Text and Binary Files (Closing) Text File I/O fclose(fp); Binary File I/O

Two other file operations functions that may be useful int remove(const char *filename); Defined in <stdio.h> Deletes the file specified by filename int rename(const char *oldname, const char *newname); Changes the name of a file from oldname to newname

Disadvantages of Binary Files A binary file created on one computer is rarely readable on another type of computer Since a binary file can be read only by a specialized computer program, a person cannot proofread the file A binary file cannot be created or modified in a word processor, so a program that expects binary file input cannot be tested until the program that produces the binary file is complete

Random Files Random file access refers to the ability to read or modify information directly at any given position in a file This is done by getting and setting a file position indicator, which represents the current access position in the file associated with a given stream

Random Access Files Records in a file created with fprintf are not necessarily the same length Individual records of a randomly accessed file are normally fixed in length and may be accessed directly (thus quickly) without searching through records This makes random access files appropriate for systems that require rapid access to specific data

Random Access Files Every record in a randomly accessed file normally has the same length The exact location of a record relative to the beginning of the file can be calculated as a function of the record key 100 bytes 100 bytes 100 bytes 100 bytes 100 bytes etc. 100 200 300 400 500 byte offsets

Random Access Files Normally, structs are used in random access files Example program A program to process bank accounts Each record has an account number used as the record key, a last name, a first name and a balance First a random access file of 100 records is created with 0 for account number, NULL for first name and last name and 0.0 for balance

Example – creating a random accessed file (from Deitel) /* Creating a randomly accessed file sequentially */ #include <stdio.h> #define NRECORDS 100 struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; };

Example – creating a random accessed file (from Deitel) int main() { int i; struct clientData blankClient = {0, "", "", 0.0}; FILE *cfPtr; if ( (cfPtr = fopen("credit.dat", "wb")) == NULL) printf("File could not be opened.\n"); for (i = 1 ; i <= NRECORDS ; i++) fwrite(&blankClient, sizeof(struct clientData), 1, cfPtr); fclose(cfPtr); return 0; }

Example – writing data randomly to a random accessed file (from Deitel) #include <stdio.h> #define NRECORDS 100 struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; }; int main() { struct clientData client = {0, "", "", 0.0}; FILE *cfPtr;

Example – writing data randomly to a random accessed file (from Deitel) if ( (cfPtr = fopen("credit.dat", "r+b")) == NULL) printf("File could not be opened.\n"); printf("Enter account number (1 to %d, 0 to end input)\n? ", NRECORDS); scanf("%d", &client.acctNum);

Example – writing data randomly to a random accessed file (from Deitel) while(client.acctNum != 0) { printf("Enter lastname, firstname, balance\n? "); fscanf(stdin, "%s%s%lf", client.lastName, client.firstName, &client.balance); fseek(cfPtr, (client.acctNum - 1) * sizeof(struct clientData), SEEK_SET); fwrite(&client, sizeof(struct clientData), 1, cfPtr); printf("Enter account number (1 to %d, 0 to end input)\n? ", NRECORDS); scanf("%d", &client.acctNum); } fclose(cfPtr); return 0;

fseek function int fseek (FILE *stream, long int offset, int whence) offset is the number of bytes from location whence in the file pointed to by stream whence can have one of three values SEEK_SET – seek starts at the beginning of the file SEEK_CUR – seek starts at the current location in the file SEEK_END – seek starts at the end of the file

Example – reading data from a random accessed file (from Deitel) #include <stdio.h> struct clientData { int acctNum; char lastName[15]; char firstName[15]; double balance; }; int main() { struct clientData client = {0, "", "", 0.0}; FILE *cfPtr;

Example – reading data from a random accessed file (from Deitel) if ( (cfPtr = fopen("credit.dat", "rb")) == NULL) printf("File could not be opened.\n"); printf("%-6s%-16s%-11s%10s\n","Acct", "Last Name", "First Name", "Balance"); while(fread(&client, sizeof(struct clientData), 1, cfPtr)) if(client.acctNum != 0) printf("%-6d%-16s%-11s%10.2f\n", client.acctNum, client.lastName, client.firstName, client.balance); fclose(cfPtr); return 0; }