1 CSC103: Introduction to Computer and Programming Lecture No 27.

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

FILES Files types and operations. Files Files are used to store data Data can be Text (ASCII only: 0  127) Binary (Full range: 0  256) Each file resides.
File Organizations Sept. 2012Yangjun Chen ACS-3902/31 Outline: File Organization Hardware Description of Disk Devices Buffering of Blocks File Records.
COSC 120 Computer Programming
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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.
Operating Systems File systems
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 3 – Computer Hardware Computer Components – Hardware (cont.) Lecture 3.
1 CSC103: Introduction to Computer and Programming Lecture No 26.
CSC 125 Introduction to C++ Programming Chapter 1 Introduction to Computers and Programming.
CHP - 9 File Structures. INTRODUCTION In some of the previous chapters, we have discussed representations of and operations on data structures. These.
Topics Introduction Hardware and Software How Computers Store Data
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.
Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and Elizabeth Drake Chapter 6: Sequential Data Files.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
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. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CSE1301 Computer Programming: Lecture 14 I/O and Files.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 14: Files and Streams. 2Microsoft Visual C# 2012, Fifth Edition Files and the File and Directory Classes Temporary storage – Usually called computer.
COMP 116: Introduction to Scientific Programming Lecture 29: File I/O.
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.
 2000 Prentice Hall, Inc. All rights reserved Introduction Data files –Can be created, updated, and processed by C programs –Are used for permanent.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 1 Introduction to Computers and Programming.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
1 CSC241: Object Oriented Programming Lecture No 32.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
Files A collection of related data treated as a unit. Two types Text
Web Page Designing With Dreamweaver MX\Session 1\1 of 9 Session 3 PHP Advanced.
 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.
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.
Real Numbers Device driver process within the operating system that interacts with I/O controller logical record 1 logical record 2 logical record 3.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
BASIC PROGRAMMING C SCP1103 (02)
11 C File Processing.
Computer Systems Nat 4/5 Computing Science
Chapter 6: Sequential Data Files
CHP - 9 File Structures.
BASIC PROGRAMMING C SCP1103 (02)
TMF1414 Introduction to Programming
File I/O.
Introduction to Computers
Operation System Program 4
Chapter 11 – File Processing
Manipulating File IO in Visual C++
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
Fundamental of Programming (C)
Files.
EPSII 59:006 Spring 2004.
Course Contents KIIT UNIVERSITY Sr # Major and Detailed Coverage Area
EECE.2160 ECE Application Programming
Files Chapter 8.
Presentation transcript:

1 CSC103: Introduction to Computer and Programming Lecture No 27

2 Previous lecture Example program – structure C preprocessor directives Macro expansion File inclusion Conditional Compilation

3 Today’s lecture outline Introduction to File File operations – Reading from file (character by character) – Writing in file (character by character)

4 File Until now we have been saving record in volatile memory i.e. RAM struct book – E.g. entering books record, display and search facility (struct book) File can be used to store data permanently i.e. on hard disk Data in files can be retrieve even the computer is powered off C programmer is able to read, write, modify and delete the content of file

5 Cont. A common data file hierarchy is typically broken down into five categories – Bit Binary : digit, 0 or 1 – Byte : Eight bits – Field : Grouping of bytes – Record : Grouping of fields – File : Grouping of records

6 Fields, Records Fields could be a name, cgpa, department, street address, phone number, and so on Records are logical groupings of fields that comprise a single row of information A student record might be comprised of name, age, ID, and GPA fields. Each field is unique in description but together describes a single record. NameIDCGPADepartment Ali Ahmad Computer science Fahad Hamid Computer science Field Record

7 Data file Data files are comprised of one or more records Files can be used to store all types of information, such as student or employee data C programmers use pointers to manage file for reading and writing data Fatima Tariq Physics Ali Ahmad Computer science Fahad Hamid Computer science

8 File Operations Creation of a new file Opening an existing file Reading from a file Writing to a file Moving to a specific location in a file (seeking) Closing a file

9 Example program Go to program

10 Opening a File Before reading and writing operation on file, file must be opened fopen() function can be used to open a file Example program opens ”prog1.c” file in read mode Read mode means content of file cannot be modified or added, they can just be read string

11 Tasks of fopen( ) function 1.Firstly it searches on the disk the file to be opened 2.Then it loads the file from the disk into a place in memory called buffer 3.It sets up a character pointer that points to the first character of the buffer It is much faster to read / write content in memory as compare to hard disk

12 Cont.

13 structure FILE For successful reading from a file information like mode of opening, size of file, place in the file from where the next read operation would be performed, etc. has to be maintained fopen( ) store such information in a FILE structure and returns it address We store that address in a pointer of type FILE

14 Reading from a File Once file is opened using fopen ( ), its content are brought into buffer A pointer is set that points to the first character in this buffer This pointer is one of the elements of the structure to which fp is pointing To read the file’s contents from memory we use a function called fgetc( )

15 Cont. ch = fgetc ( fp ) ; fgetc( ) reads the character from the current pointer position advances the pointer position so that it now points to the next character, and returns the character that is read *fp hellow orld ch = fgetc ( fp ) ; ch 406 h

16 Cont. Once the file is opened, we can refer file not by its name but through FILE pointer The function fgetc( ) within an indefinite while loop Loop will be executed until if finds End Of File (EOF) EOF is a special character whose ASCII value is 26 EOF is inserted after the last character in the file EOF is macro has been defined in the file stdio.h. hellow orld 405 EOF

17 Trouble in Opening a File There is a possibility that when we try to open a file using the function fopen( ), the file may not be opened For example, the file may not exist on the disk Other reasons may be disk space may be insufficient to open a new file, or the disk may be write protected or the disk is damaged and so on If the file opening fails the fopen( ) function returns a value NULL

18 Example code This check must be performed for every program that involve File I/O

19 Closing the File After reading/writing from the file, file should be closed Once file is closed, it is not possible to perform read/write operation Three operations would be performed by fclose( ) – The characters in the buffer would be written to the file on the disk – At the end of file a character with ASCII value 26 would get written (EOF character) – The buffer would be eliminated from memory

20 Example program Write a program that will read a file and count how many characters, spaces, tabs and newlines are present in it write a program

21 Writing character in file fputc( ) fputc( ) is used to writes character in a file fputc ( ch, fp ) ; fputc write the character constant store in character variable in the buffer that is pointer by a pointer fp Character variableFile pointer

22 A File-copy Program Write a program that create a copy of a file. The user input two file names – the name of file to be copied – Copy file name write a program

23 File Opening Modes "r" : reading from file, if file does not exist it returns null "w" : writing to the file, if file does not exits a new file is created "a" : adding new contents at the end of file, if file does not exits a new file is created

24