CSEB114: Principle of programming Chapter 11: Data Files & File Processing.

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

BITS Pilani, Pilani Campus TA C252 Computer Programming - II Vikas Singh File Handling.
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 11: Data Files & File Processing In this chapter, you will learn about Files and streams Creating a sequential access file Reading data from a.
KUKUM Sem2-05/06EKT120: Computer Programming1 Week 11 – Files.
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 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.
File I/O.
CSE1301 Computer Programming: Lecture 19 File I/O
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
Lecture 11 – Files Operation. Introduction Almost all of the program developed before this is interactive In interactive environment, input is via keyboard.
© Janice Regan, CMPT 128, Jan CMPT 128: Introduction to Computing Science for Engineering Students C File Input and Output Checking input for errors.
File Handling Spring 2013Programming and Data Structure1.
 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.
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.
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 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: 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.
 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)
Structured Programming Approach Module VIII - Additional C Data Types File Handling Prof: Muhammed Salman Shamsi.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
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. 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.
UniMAP Sem2-09/10 DKT121: Fundamental of Computer Programming1 Files Operations.
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.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
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.
6/9/2016Course material created by D. Woit 1 CPS 393 Introduction to Unix and C START OF WEEK 10 (C-4)
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
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.
TMF1414 Introduction to Programming
EKT120: Computer Programming
File I/O.
Plan for the Day: I/O (beyond scanf and printf)
CSE1320 Files in C Dr. Sajib Datta
CSE1320 Files in C Dr. Sajib Datta
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
File I/O We are used to reading from and writing to the terminal:
Beginning C Lecture 11 Lecturer: Dr. Zhao Qinpei
FILE HANDLING IN C.
File Input and Output.
File Handling.
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)
Files.
EPSII 59:006 Spring 2004.
FILE handeling.
Files Operations.
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:

CSEB114: Principle of programming Chapter 11: Data Files & File Processing

Objectives prepared by NI, edited by MAF  In this chapter, you will learn about  Files and streams  Creating a sequential access file  Reading data from a sequential access file  Using fgetc() and fputc()  Using fgets() and fputs()  Using fprintf() and fscanf()  Using fopen() and fclose()

Files and Streams prepared by NI, edited by MAF  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 n -1

Files and Streams prepared by NI, edited by MAF  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.

Declaring a file prepared by NI, edited by MAF  The standard library provides some of the file manipulation function.  Declaring file: FILE *the_file;  States that thefile is a pointer to a file structure  If there is more than one file, each file needs to have its own FILE pointer.  thefile is an internal file name used by the program to refer to the external file name (the actual physical file stored on disk).

File operation: fopen () prepared by NI, edited by MAF  Before we can process a file, we must either open or create it.  Syntax for fopen( ): internal_file_name = fopen(external_file_name, OpenMode);  The fopen() function takes 2 arguments: the file name and the file mode.

File operation: fopen () prepared by NI, edited by MAF  Example: thefile = fopen(“my_file.txt”,”r”);  This statement will try to open a file called “my_file.txt”.  my_file.txt is the external file name referring to a phisycal file on the disk.  If the file is to be placed in a different directory than the program directory, the full path need to be specified. For example: “D:/my_file.txt”  The function returns a pointer to the successfully opened file. But if the file cannot be opened, a NULL is returned.

File OpenMode: prepared by NI, edited by MAF  The following is the list of open mode that can be associated with fopen( ).  “r” : to open existing file in input mode so data can be read from it. The file to be opened must exist phisically on disk for this open mode to be successful.  “w” : the file is intended to be an output file and we are planning to write data to it.  “a” : to append data to the end of existing file.  “r+” : to open existing file for update (both in input and output mode). If the file already exist, the content is not destroy.  “w+” : destroy the file if already exist and open a new file for update.  “a+” : open file for update (adding data at the end of the file.

fopen( ) prepared by NI, edited by MAF  You could also ask the users to specify the name of the file s/he want to open.  Example: char filename[50]; printf("Enter file name: "); gets(filename); thefile = fopen(filename,"r");  The users must enter the full filename, including the extension (.txt,.doc etc) and path.

File operation: fail fopen( ) prepared by NI, edited by MAF  If fopen( ) is returning a NULL value, this means that the fopen( ) has failed.  This is due to any of the following reasons:  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.  Therefore, in our program, we need to write statements that would handle this failure.

fputc( ) prepared by NI, edited by MAF  fputc( ) is a function that would write a character value to the file.  Syntax:  Using character variable, ch: fputc(ch, internal_file_name);  Example: char m = ‘c’; fputc(m, theFile);  Using character constant: fputc(‘a’,internal_file_name);  Example fputc(‘c’, theFile);

fputc() example prepared by NI, edited by MAF #include int main(void) { char ch; FILE *thefile; if ((thefile = fopen("file_putc.txt","w")) == NULL) { printf("File fail to open\n"); } else { printf("Enter a character: "); scanf("%c", &ch); fputc(ch, thefile); } fclose(thefile); return 0; }

fgetc( ) prepared by NI, edited by MAF  fgetc( ) is a function that would read a character from a file.  Syntax: variable = fgetc(internal_file_name);  Where:  variable is a character variable to hold the value returned by fgetc( ).  Internal_file_name is the pointer to an open file.  Example: char c; fgetc(theFile);

fgetc( ): Example prepared by NI, edited by MAF #include int main(void) { char ch; FILE *thefile; if ((thefile = fopen("file_getc.txt","r")) == NULL) { printf("File fail to open\n"); } else { ch = fgetc(thefile); /*read a character from the file */ printf("%c\n",ch);/* print the value of ch to the screen */ } fclose(thefile); return 0; }

fprintf( ) prepared by NI, edited by MAF  fprintf( ) is a function that would print values to the file  Syntax: fprintf(internal_name, variable expression);  Example 1: fprintf(theFile, “This is an example”);  Example 2: char name[50] = “Ahmad” fprintf(theFile, name);  Example 3: char name[50] = “Ahmad”; fprintf(theFile, “My name is” name);

fprintf( ): Example prepared by NI, edited by MAF #include int main() { int i, power; FILE *thefile; if((thefile = fopen("file_fprintf.txt","w"))==NULL) printf("File could not be open\n"); else{ for(i=1;i<=100;i++){ power=i*i; fprintf(thefile,"%d\n",power); } printf("End of operations"); } fclose(thefile); return 0; }

fscanf( ) prepared by NI, edited by MAF  fscanf( ) is a function that read a value, or values of mix data type from a file.  Syntax: fscanf(internal_name,formatControl, variableList);  Example: char name[50]; int age; fscanf(theFile, “%s %d”, name, &age);

fscanf( ): Example prepared by NI, edited by MAF #include void main() { int i, bil, matrik; char grade[2]; char name[10]; FILE *thefile; if((thefile = fopen("file_fscanf.txt","r")) == NULL) printf("File could not be open"); else{ fscanf(thefile,"%d",&bil); printf("%-10s%-10s%s\n","Matrik","Name","Grade"); for(i=0;i<bil;i++) { fscanf(thefile,"%d%s%s",&matrik,name,&grade); printf("%-10d%-10s%s\n",matrik,name,grade); } fclose(thefile); }

fputs() prepared by NI, edited by MAF  Writes the string to the file.  The function begins outputting the string until it reaches the terminating null character ('\0').  Syntax fputs (string variable, internal name);  Example: char str [20] = “gummy bear”; fputs (str, theFile);

fputs( ): Example prepared by NI, edited by MAF #include int main() { char str[20]= "let's picnic!"; FILE *thefile; if((thefile = fopen("file_fputs.txt","w"))==NULL) printf("File could not be open\n"); else fputs(str, thefile); fclose(thefile); return 0; }

fgets( ) prepared by NI, edited by MAF  fgets( ) read a string of characters from a file.  Syntax: fgets(stringVariable, size, internal_name);  Example: char content[50]; fgets(content, 50, theFile);  fgets( ) will read size – 1 character from the file pointed by theFile, and saves the string into variable content.

fgets( ): Example prepared by NI, edited by MAF #include int main() { char str[30]= {'\0'}; FILE *thefile; if((thefile = fopen("file_fgets.txt","r"))==NULL) printf("File could not be open\n"); else{ fgets(str, 30, thefile); puts(str); } fclose(thefile); return 0; }