Introduction to Programming

Slides:



Advertisements
Similar presentations
Memory and Files Dr. Andrew Wallace PhD BEng(hons) EurIng
Advertisements

Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Structures and Unions Chapter 6. Structure A structure is an aggregate data type  Composed of two or more related variables called member/field/element.
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.
 Types of files  Command line arguments  File input and output functions  Binary files  Random access.
Applications with Random File Access CHAPTER 8. C.13 1 Where am I on the Binary File? » While a binary file is processed, it is a need to know current.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
A First Book of ANSI C Fourth Edition Chapter 10 Data Files.
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.
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.
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.
1 Lecture09: File I/O 5/6/2013 Slides modified from Yin Lou, Cornell CS2022: Introduction to C.
Text and Binary File Processing 程式設計 潘仁義 CCU COMM.
“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.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 4.
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.
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 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
C Programming Lecture 12 : File Processing
1 CSC241: Object Oriented Programming Lecture No 32.
Learners Support Publications Working with Files.
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)
Adv. UNIX:fp/101 Advanced UNIX v Objectives of these slides: –a more detailed look at file processing in C Special Topics in Comp. Eng.
 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.
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.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Lecture 12 CIS 208 Friday, March 3rd , 2005.
External Files: Abstractly, a file can be thought of as a stream of data (either char or binary). C has two groups of files: standard files, such as stdin,
Chapter 4 File Processing
Chapter 22 – part a Stream refer to any source of input or any destination for output. Many small programs, obtain all their input from one stream usually.
Lecture 11 File input/output
Tutorial4us.com File Handling in C++ Tutorial4us.com.
TMF1414 Introduction to Programming
Introduction to Computer Programming Lecture 18 Binary Files
File I/O.
Session #5 File I/O Bit Masks, Fields & Bit Manipulations
CGS 3460, Lecture 41 Apr 21, 2006 Hen-I Yang
File Handling in C.
CSC215 Lecture Input and Output.
C Programming:Part 3 Characters and Strings File Processing Exercise.
CS111 Computer Programming
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
What you need for the 1st phase of project
File Handling in C.
Introduction to Programming
קבצים קרן כליף.
CSE1320 Files in C Dr. Sajib Datta
Chapter 11 – File Processing
Lecture 15 Files.
CSC215 Lecture Input and Output.
CSC215 Lecture Input and Output.
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.
Text and Binary File Processing
File Handling in C.
Fundamental of Programming (C)
Chapter 12: File I/O.
Applications with Random File Access
A First Book of ANSI C Fourth Edition
Block I/O fread and fwrite functions are the most efficient way to read or write large amounts of data. fread() – reads a specified number of bytes from.
Professor Jodi Neely-Ritz University of Florida
I/O CS580U - Fall 2018.
Programming Fundamental
Presentation transcript:

Introduction to Programming Lecture 19

Random Access Files

Files open ( file_name , mode ) ; close ( ) ;

ifstream myFilePtr ; myFilePtr.open ( “myFile” , ios :: in ) ;

Output File Stream ios :: app ios :: trunc ios :: ate

Read/write a character get ( ) Read a character put ( ) Write a character

Number of characters to read getline(str,10, ‘\n’) ; Number of characters to read Delimiter Storage Requirements for Text and Binary Modes Storage Requirements for Text and Binary Modes for an Integer: for an Integer: 00110101 00110101 00111001 00111001 Page 32 Page 32 The fwrite() Function The fwrite() Function size_t fwrite (void *ptr, size_t size, size_t nmemb, size_t fwrite (void *ptr, size_t size, size_t nmemb, FILE *fp); FILE *fp); fwrite() writes, from the address pointed to by ptr, fwrite() writes, from the address pointed to by ptr, up to nmemb elements whose size is specified by up to nmemb elements whose size is specified by size, to the file pointed by fp size, to the file pointed by fp fwrite() returns the number of elements fwrite() returns the number of elements successfully written, which will be less than nmemb successfully written, which will be less than nmemb only if a write error is encountered. only if a write error is encountered. double earnings[10]; double earnings[10]; fwrite(earnings,sizeof(double),10,fp); fwrite(earnings,sizeof(double),10,fp); 32 32 Page 33 Page 33 The fread() Function The fread() Function size_t fread(void *ptr, size_t size, size_t nmemb, size_t fread(void *ptr, size_t size, size_t nmemb, FILE *fp); FILE *fp); fread() reads, into the array pointed to by ptr, up to fread() reads, into the array pointed to by ptr, up to nmemb elements whose size is specified by size, nmemb elements whose size is specified by size, form the file pointed to by fp. form the file pointed to by fp. fread() returns the number of elements fread() returns the number of elements successfully read, which may be less than nmemb successfully read, which may be less than nmemb is a read error or end-of-file is encountered. is a read error or end-of-file is encountered. 33 33 Page 34 Page 34 Writing file data using block I/O functions Writing file data using block I/O functions #include <stdio.h> #include <stdio.h> typedef struct { typedef struct { char name[20]; char name[20]; int serial_code; int serial_code; int amount; int amount; float float cost; cost; } component; } component; main(void) main(void) { component comp_data; { component comp_data; FILE FILE *fp; *fp; char char filename[80]; filename[80]; int numofcomp; int numofcomp; int i; int i; printf("Enter the file name: "); printf("Enter the file name: "); gets(filename); gets(filename); 34 34 Page 35 Page 35 if ((fp = fopen(filename, "wb")) == NULL) { if ((fp = fopen(filename, "wb")) == NULL) { printf("can't open file \n"); printf("can't open file \n"); exit(1); exit(1); }printf("Enter the number of components: "); }printf("Enter the number of components: "); scanf("%d", &numofcomp); scanf("%d", &numofcomp); for (i=0; i<numofcomp; i++) { for (i=0; i<numofcomp; i++) { printf("Name of the component: "); printf("Name of the component: "); gets(comp_data.name); gets(comp_data.name); printf("Serial code of the component: "); printf("Serial code of the component: "); scanf("%d", &comp_data.serial_code); scanf("%d", &comp_data.serial_code); printf("Amount of the component: "); printf("Amount of the component: "); scanf("%d", &comp_data.amount); scanf("%d", &comp_data.amount); printf("Cost of each component: "); printf("Cost of each component: "); scanf("%f", &comp_data.cost); scanf("%f", &comp_data.cost); fwrite(&comp_data, sizeof(comp_data), 1, fp); fwrite(&comp_data, sizeof(comp_data), 1, fp); }fclose(fp); }fclose(fp); return 0; return 0; } } 35 35 Page 36 Page 36 12.5 Random Access 12.5 Random Access Random Access: fseek() and ftell() Random Access: fseek() and ftell() fseek, ftell: All the previous I/O functions do reading fseek, ftell: All the previous I/O functions do reading and writing sequentially, i.e. read the 1st datum, 2rd and writing sequentially, i.e. read the 1st datum, 2rd datum, 3rd datum ..., etc. datum, 3rd datum ..., etc. fseek fseek and and ftell ftell help doing help doing I/O in non-sequential manner, i.e. read the 10th I/O in non-sequential manner, i.e. read the 10th datum, then go back and read the 2nd datum. datum, then go back and read the 2nd datum. 36 36 Page 37 Page 37 File position pointer File position pointer The system keeps a file position pointer for The system keeps a file position pointer for each open file. each open file. It indicates the location in the file at It indicates the location in the file at which data will be read or written. which data will be read or written. file position marker file position marker 0 10 20 30 40 50 60 70 80 90 0 10 20 30 40 50 60 70 80 90 ...... ...... Disk Disk 37 37 Page 38 Page 38 The fseek() Function The fseek() Function The function prototype of The function prototype of fseek() fseek() is is int fseek(FILE *fp, long int offset, int mode); int fseek(FILE *fp, long int offset, int mode); * * fseek() fseek() returns returns if OK, and if OK, and -1 -1 if there is an error. if there is an error. * * offset offset tells how far to move from the starting point tells how far to move from the starting point (depending on the mode). It can be +ve (move (depending on the mode). It can be +ve (move forward) or -ve (move backward) or 0. forward) or -ve (move backward) or 0. * * mode mode identifies the starting point. In stdio.h, the identifies the starting point. In stdio.h, the following constants can be assigned to mode: following constants can be assigned to mode: Mode Mode Measure offset from Measure offset from SEEK_SET SEEK_SET Beginning of file Beginning of file SEEK_CUR SEEK_CUR Current position Current position SEEK_END SEEK_END End of file End of file 38 38 Page 39 Page 39 The ftell() Function The ftell() Function The function prototype of The function prototype of ftell() ftell() is is long int ftell(FILE *fp); long int ftell(FILE *fp); returns the current file location. returns the current file location. The rewind() Function The rewind() Function The function prototype of The function prototype of rewind() rewind() is is void rewind(FILE *fp); void rewind(FILE *fp); Resets the file position marker to the beginning of Resets the file position marker to the beginning of the file. This is equivalent to the file. This is equivalent to fseek(fp, 0L, SEEK_SET); fseek(fp, 0L, SEEK_SET); 39 39 Page 40 Page 40 /* reverse.c - displays a file in reverse order */ /* reverse.c - displays a file in reverse order */ #include <stdio.h> #include <stdio.h> #include <stdlib.h> #include <stdlib.h> #define CNTL_Z '\032' #define CNTL_Z '\032' /* eof marker in DOS textfiles */ /* eof marker in DOS textfiles */ { int main(int argc, char *argv[]) { int main(int argc, char *argv[]) char ch; char ch; FILE *fp; FILE *fp; long count, last; long count, last; if ((fp=fopen(argv[1],"rb"))== NULL) { if ((fp=fopen(argv[1],"rb"))== NULL) { printf("reverse can't open %s\n", argv[1]); printf("reverse can't open %s\n", argv[1]); exit(1); exit(1); } } fseek(fp,0L, SEEK_END); fseek(fp,0L, SEEK_END); /* goto eof */ /* goto eof */ last=ftell(fp); last=ftell(fp); for (count=1L; count<=last; count++) { for (count=1L; count<=last; count++) { fseek(fp,-count,SEEK_END); fseek(fp,-count,SEEK_END); ch=getc(fp); ch=getc(fp); if (ch!=CNTL_Z && ch!= '\r') if (ch!=CNTL_Z && ch!= '\r') putchar(ch); putchar(ch); }fclose(fp); }fclose(fp); return(0); return(0); 40 40 } }

File Positions

File Position Pointer

myFile.tellg ( ) ; tellg ( ) Function Returns a whole number which tell you the position of the next character to be read from the file

tellp ( ) Function myFile.tellp ( ) ; Returns a whole number which tell you the position of the next character to be written in a file

For Positioning in the file seekg ( ) ; seekp ( ) ;

filePtr.seekg ( long Num , ios :: origin ) ; Number of characters to move to Starting point filePtr.seekg ( long Num , ios :: origin ) ;

seekg ( ) seekg ( 10L , ios :: beg ) ; seekg (10L , ios :: cur ) ; seekg ( 10L , ios :: end ) ;

Example 1 #include<fstream.h> main ( ) { int length ; ifstream inFile ( “myFile.txt” ) ; inFile.seekg ( 0L , ios :: end ) ; length = inFile.tellg ( ) ; }

File Name city Date-of-Birth : : : Jamil Ahmed Sukkur 10-10-1982 : : : : : : Jamil Ahmed Sukkur 10-10-1982 : : : Rawalpindi

Merge Method Original file Empty file This is a text data And needs To be replaced NOT

seekg ( ) seekg ( 2201L , ios :: beg ) ;

fstream myFile ( “Sample.txt” , ios :: in | ios :: out ) ;

OR Function A B Output 0 0 0 0 1 1 1 0 1 1 1 1

Example 2 This is an Apple This is a Sample

get ( ) and put ( ) character in a file myInputFile.get ( c ) ; myOutputFile.put ( c ) ;

read ( ) and write ( ) Functions Area in memory Number of bytes to be read read ( char *buff , int count ) ; write ( char *buff , int count ) ; Area in memory Number of bytes to be written

Example 3 char str [ 10000 ] ; myInputFile.read ( str , 10000 ) ; myOuputFile.write ( str , 10000 ) ;

seekg ( ) seekg ( 0L , ios :: end ) ;

seekg ( ) seekg ( -1L , ios :: end ) ;

seekg ( ) seekg ( -2L , ios :: cur ) ;

myOutputFile.write ( &i , 4 ) ; Address of the integer ‘i’ Number of bytes to be written myOutputFile.write ( &i , 4 ) ;

sizeof ( ) ;

myOutputFile.write ( &i , sizeof ( i ) ) ; Address of the integer ‘i’ Size of integer myOutputFile.write ( &i , sizeof ( i ) ) ;

for ( i = 0 ; i < 100 ; i ++ ) { myOutputFile.write ( &i , sizeof ( i ) ) ; } myOutputFile.close ( ) ;