1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University

Slides:



Advertisements
Similar presentations
File Management in C. What is a File? A file is a collection of related data that a computers treats as a single unit. Computers store files to secondary.
Advertisements

Chapter 12: File System Implementation
UNIT 15 File Processing.
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.
Files in C Rohit Khokher. Files in C Real life situations involve large volume of data and in such cases, the console oriented I/O operations pose two.
Chapter 4 : File Systems What is a file system?
Chapter 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Accessing Files in CCS-2303, C-Term Accessing Files in C CS-2303 System Programming Concepts (Slides include materials from The C Programming Language,
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
Rensselaer Polytechnic Institute CSCI-4210 – Operating Systems David Goldschmidt, Ph.D.
Computer Studies (AL) File Management File system interface.
Chapter 11: File System Implementation Hung Q. Ngo KyungHee University Spring 2009
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.
Component 4: Introduction to Information and Computer Science Unit 4: Application and System Software Lecture 3 This material was developed by Oregon Health.
File Systems CSCI What is a file? A file is information that is stored on disks or other external media.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 7P. 1Winter Quarter File I/O in C Lecture.
The Operating System ICS3M.  The operating system (OS) provides a consistent environment for other software programs to execute commands.  It gives.
Lecture 26: FILING HANDLING in C Language FILING HANDLING in C Language-1 Lecture 26.
Introduction to Programming Using C Files. 2 Contents Files Working with files Sequential files Records.
CSE1301 Computer Programming: Lecture 14 I/O and Files.
Lecture 8a: File I/O BJ Furman 21MAR2011. Learning Objectives Explain what is meant by a data stream Explain the concept of a ‘file’ Open and close files.
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 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
CNG 140 C Programming (Lecture set 10) Spring Chapter 10 Data Files.
File Systems. 2 What is a file? A repository for data Is long lasting (until explicitly deleted).
FAT File Allocation Table
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
CS 261 – Recitation 7 Spring 2015 Oregon State University School of Electrical Engineering and Computer Science.
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
SOCSAMS e-learning Dept. of Computer Applications, MES College Marampally FILE SYSTEM.
1 CSC103: Introduction to Computer and Programming Lecture No 27.
Files A collection of related data treated as a unit. Two types Text
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand the basic properties and characteristics of external files ❏ To.
CSC Programming for Science Lecture 18: More Data Files.
Lecture 20: C File Processing. Why Using Files? Storage of data in variables and arrays is temporary Data lost when a program terminates. Files are used.
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.
File System Department of Computer Science Southern Illinois University Edwardsville Spring, 2016 Dr. Hiroshi Fujinoki CS 314.
Silberschatz and Galvin  C Programming Language Kingdom of Saudi Arabia Ministry of Higher Education Al-Majma’ah University College of Education.
Chapter 7 Text Input/Output Objectives
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.
Chapter 7 Text Input/Output Objectives
Structured Programming II
File Access (7.5) CSE 2031 Fall July 2018.
File I/O.
CSC215 Lecture Input and Output.
CSE1320 Files in C Dr. Sajib Datta
File Input/Output.
CSE1320 Files in C Dr. Sajib Datta
File Management.
CS-401 Computer Architecture & Assembly Language Programming
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
File Systems Implementation
Seek Method and CSV Files
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
Seek Method and CSV Files
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
File Access (7.5) CSE 2031 Fall January 2019.
Accessing Files in C Professor Hugh C. Lauer CS-2303, System Programming Concepts (Slides include materials from The C Programming Language, 2nd edition,
Fundamental of Programming (C)
File Access (7.5) CSE 2031 Fall February 2019.
EPSII 59:006 Spring 2004.
A First Book of ANSI C Fourth Edition
Department of Computer Science
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Files Chapter 8.
Presentation transcript:

1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University Spring 2009

Files and File Processing Lecture 11

Files and File Processing Files are commonly used for the permanent storage of large quantities of data. Files are saved on a secondary storage device such as a hard drive or a floppy disk; these disks can hold files of different types: Text files: lab_report.doc Program files: lab.c Executable files: lab.exe Data files: result.dat Files are usually partitioned and written over many tracks and sectors of a disk. Disk Track Sector

The operating system maintains a directory tree that lists all files stored on all disks, maintains a File Allocation Table (FAT) that associates a file to its physical location(s) on the disk, and activates and maintains the File Control Bloc (FCB) that allows a program to access a file stored on a disk. A file can be written or read by a C program. Standard C functions are available and can be used to create and manipulate files. A file is viewed in C as a sequence of bytes. When a file is opened, a stream is associated with the file. A stream can be viewed as a communication channel between the program and the opened file. Files and File Processing

A file is accessed in C using a pointer to a file. If more than one file must be opened by the same program, then each file must have its own file pointer. A file pointer is declared as follows: FILE *filePtr; The type FILE is defined in stdio.h The type FILE has all of the system dependant information required to allow a C program to easily manipulate a file. The type FILE is linked to the File Control Bloc. Opening and Closing Files

A pointer to a file is assigned to a specific file using the standard C function fopen : E.g.: filePtr = fopen( “ test.dat ”, “ w ” ); fopen requires two arguments:  the name of the file to be opened, in this example the name is test.dat, and  the file open mode, in this example the mode is w A pointer must be assigned to a file using fopen before data can be read from the file or written to the file; fopen opens the file and establishes the stream required to access it. Opening and Closing Files

There exists many file open modes. The mode that best suits the type of file access required is selected. w write: used to create a new file or to erase an existing file and to write in it. r read: used to read an existing file. a append: used to write data at the end of a file. w+ write+: used to create a new file or to erase the content of an existing file, and then to write to and to read from the file. r+ read+: used to read from and to write to the file. a+ append+: used to read from a file and to write at the end of the file. Opening and Closing Files

Careful: a particularly nasty error is to open a file using the file open mode w for reading.  Not only will reading be impossible in this mode but the content of the file will also be erased! If there was an error opening the file then fopen returns the NULL address. E.g.: FILE *filePtr; filePtr = fopen( “ c:\myfiles\test.dat ”, “ r ” ); if(filePtr == NULL) printf( “ Error encountered opening file.\n ” ); ! Opening and Closing Files

A file can be closed by invoking the C standard function fclose E.g.: fclose(filePtr); Normally, a file is closed as soon as we are done accessing it. fclose receives one argument: the pointer to the file to be closed. fclose is of type int. fclose returns 0 if it has successfully closed the file, or EOF if an error was encountered. If files are not closed when execution of main() ends then the operating system will close all opened files. It is recommended that all files opened by a program be closed as soon as they are no longer needed since this releases the resources required to manage open files. Opening and Closing Files