Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()

Slides:



Advertisements
Similar presentations
I/O means Input and Output. One way: use standard input and standard output. To read in data, use scanf() (or a few other functions) To write out data,
Advertisements

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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.
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.
File processing with functions H&K Chapter 3 Instructor – Gokcen Cilingir Cpt S 121 (June 27, 2011) Washington State University.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 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.
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.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Files Programs and data are stored on disk in structures called files Examples Turbo C++ - binary file Word binary file lab1.c - text file lab1.data.
1 Structure part 1 & File Processing. 2 Structures.
C How to Program, 7/e © by Pearson Education, Inc. All Rights Reserved.
File Handling Spring 2013Programming and Data Structure1.
 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.
1. Introduction File Declaration and Initialization Creating and Opening File Closing File EOF Reading from and Writing into a File Extra : Random Access.
File IO and command line input CSE 2451 Rong Shi.
Chapter 8 File-Oriented Input and Output. 8.1 INTRODUCTION a file can also be designed to store data. We can easily update files, A data file as input.
CSEB114: Principle of programming Chapter 11: Data Files & File Processing.
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:
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.
© 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.
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.
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)
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
Files A collection of related data treated as a unit. Two types Text
 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.
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.
1 Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating a Sequential Access File 11.5Reading.
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.
Chapter 4 File Processing
TMF1414 Introduction to Programming
What you need for the 1st phase of project
File I/O We are used to reading from and writing to the terminal:
Chapter 11 – File Processing
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
Fundamental of Programming (C)
EPSII 59:006 Spring 2004.
Module 12 Input and Output
Chapter 11 Files chap8.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
File I/O We are used to reading from and writing to the terminal:
Presentation transcript:

Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc() and fputc() Using fread() and fwrite()

Files and Streams C views a file as a sequential stream of bytes. A file ends either with an EOF (end-of-file) marker or at a specified byte number specified by the system. When a file is opened, a stream is associated with a file. Streams provide communication channels between files and the programs.

In addition to providing access to a file, a stream can also be used to access devices. For example, when a program (any program) is executed, 3 streams are automatically opened: –standard input (stdin) enable the program to read data from keyboard –standard output (stdout) enable the program to print data on the screen –standard error (stderr) enable program to print errors on the screen They are all manipulated using file pointers.

Creating a Sequential Access File #include void main (void) { int account; char name[30]; float balance; FILE *f; if ((f = fopen(“clients.txt”, “w”)) == NULL) printf(“Error: the file cannot be opened\n”); else { printf (“Enter the account, name and balance or EOF to end input:\n”); scanf (“%d%s%f”, &account, name, &balance); while (!feof(stdin)) { fprintf(f, “%d %s %.2f”, account, name, balance); scanf (“%d%s%f”, &account, name, &balance); } fclose (f); }

FILE *f; –States that f is a pointer to a file structure –If there is more than one file, each file needs to have its own FILE pointer. if ((f = fopen(“clients.txt”, “w”)) == NULL) –The fopen() function takes 2 arguments: the file name and the file mode. –This statement will try to open a file named “clients.txt”. –If the file is to be placed in a different directory than the program directory, the full path need to be specified. For example: “A:\clients.txt” –The function returns a pointer to the successfully opened file. But if the file cannot be opened, a NULL is returned. –The option “w” is the mode of the file to be opened.

while (!feof(stdin)) –Determines whether the input from the keyboard is an EOF character or not. If it is an EOF character, the while loop will terminate. –An EOF character is not the word “EOF” !!!

fprintf (f, “%d %s %.2f\n”, account, name, balance); –Writes data to the file clients.dat –Equivalent to printf() except that it receives as an argument a file pointer for the file to which the data will be written. fclose(f); –Closes the file “clients.txt”. –Will actually write the data to the file. –If fclose() is not called explicitly, the operating system will close the file when the program terminates.

If an error occurs while opening a file (in which case fopen() returns a NULL), it could be due to any of these errors: –Opening a non-existing file for reading –Opening a file for reading or writing without having granted the appropriate access to the file by the operating system. –Opening a file for writing when no disk space is available. Always remember that the mode “w” will overwrite the current data in the file. When we want to update a file, always use the update mode “r+”.

Reading Data from a Sequential Access File void main (void) { int account; char name[30]; float balance; FILE *fptr; if ((fptr = fopen(“clients.txt”, “r”)) == NULL) printf(“Error: the file cannot be opened\n”); else { printf (“%-10s%-13s%s\n”, “Account”, “Name”, “Balance”); fscanf (fptr, “%d%s%f”, &account, name, &balance); while (!feof(fptr)) { printf (“%-10s%-13s%s\n”, “Account”, “Name”, “Balance”); fscanf (fptr, “%d%s%f”, &account, name, &balance); } fclose (fptr); }

if ((fptr = fopen(“clients.txt”, “r”)) == NULL) ; –Attempts to open the file clients.txt for reading and determines whether the file is opened successfully. Notice that the mode used is “r”. fscanf (fptr, “%d%s%f”, &account, name, &balance); –Reads data from the file clients.txt –Equivalent to function scanf() except that it receives as an argument a file pointer for the file from which the data is read.

Using fgetc() and fputc() Syntax: –int fgetc(FILE *f) –fputc(int c, FILE *f) fgetc() reads a character from the file referred to by the file pointer f. fputc() puts the character c into the file referred to by the file pointer f. If the file has reached the end, fgetc() will return the EOF character.

Using fread() and fwrite() Syntax: –size_t fread (void *d, size_t s, size_t n, FILE *f) –size_t fwrite (const void *d, size_t s, size_t n, FILE *f) fread() reads n items of size s into the memory pointed to by d. The function returns the total size of data that has been read. fwrite writes n items of size s from the memory pointed to by d. The function returns the total size of data that has been successfully written.