Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana.

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.
File Management in C. Console oriented Input/Output Console oriented – use terminal (keyboard/screen) scanf(“%d”,&i) – read data from keyboard printf(“%d”,i)
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.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
File I/O.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
N305: C Programming Copyright ©2006  Department of Computer & Information Science File Handling in C.
Lone Leth Thomsen Input / Output and Files. April 2006Basis-C-8/LL2 sprintf() and sscanf() The functions sprintf() and sscanf() are string versions of.
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. 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.
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.
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.
Memory Layout, File I/O Bryce Boe 2013/06/27 CS24, Summer 2013 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.
© 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.
FILE IO in ‘C’ by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
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)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
ME-2221 COMPUTER PROGRAMMING Lecture 18 FILE OPERATIONS Department of Mechanical Engineering A.H.M Fazle Elahi Khulna University of engineering & Technology.
1 CSC103: Introduction to Computer and Programming Lecture No 28.
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.
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.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
C Programming Files I/O
TMF1414 Introduction to Programming
File I/O.
File Handling in C.
CS111 Computer Programming
What you need for the 1st phase of project
File Handling in C.
FILE HANDLING.
File Management in C.
Files I/O, Streams, I/O Redirection, Reading with fscanf
CSE1320 Files in C Dr. Sajib Datta
Lecture 13 Input/Output Files.
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
FILE HANDLING IN C.
Text and Binary File Processing
File Input and Output.
File Handling.
File Handling in C.
Fundamental of Programming (C)
C Input / Output Prabhat Kumar Padhy
Chapter 5 File Handling in C
Chapter 12: File I/O.
ETE 132 Lecture 8 By Munirul Haque.
CSc 352 File I/O Saumya Debray Dept. of Computer Science
Professor Jodi Neely-Ritz University of Florida
Presentation transcript:

Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana

Files Programming In C++, Lecture 10 By Umer Rana Disk I/O operations are performed on entities called files. A files is a collection of bytes that is given a name. In most microcomputers systems a files are used as a unit if storage on secondary storage.

Standard & System I/O Programming In C++, Lecture 10 By Umer Rana Standard I/O As the name, is the most common way of performing I/O in C programs. It has a wider range of commands, and in many respects is easier to use than system I/O. a.Character I/O b.String I/O c.Formatted I/O d.Record I/O System I/O Provides fewer ways to handle data than standard I/O. The technique it employs are very much like those used by the operating system. Thus, in some ways, system I/O is harder to program than standard I/O.

Categories of Disk I/O Programming In C++, Lecture 10 By Umer Rana Character I/O getc( ) putc( ) String I/O fgets( ) fputs( ) Formatted I/O fscanf( ) fprintf( ) Record I/O fread( ) fwrite( )

Syntax Programming In C++, Lecture 10 By Umer Rana The fopen ( ) function opens a file whose name is pointed to by ‘filename’ and returns the stream that is associated with it. The type of operation that will be allowed on the file are defined by the vale of mode. Declaration: FILE *fptr; fptr=fopen(“filename”, mode); OR FILE * fptr=fopen (“filename”, mode);

Possibilities File Modes Programming In C++, Lecture 10 By Umer Rana File TypeMeaning “r”Open an existing file for reading only “w” Open a new file for writing only. If a file with the specified file-name currently exists, it will be destroyed and a new file created in its place. “a” Open an existing file for appending (i.e., for adding new information at the end of the file). If a file with the specified file-name currently does not exist, a new file will be created. “r+”Open an existing file for both reading and writing. “w+” Open a new file for both reading and writing. If a file with the specified file-name currently exists, it will be destroyed and a new file created in its place. “a+” Open an existing file for both reading and appending. If a file with the specified file-name currently does not exist, a new file will be created.

Open a File Programming In C++, Lecture 10 By Umer Rana Before we can write a file to a disk, or read it, we must open it. Opening a file establishes an understanding between our program and the operating system about which file we’re going to access and how we’re going to do it. The fopen() function returns a pointer to the FILE structure for our file, which we store in a variable fptr.

Write to a File Programming In C++, Lecture 10 By Umer Rana Once we’ve established a connection with a particular file by opening it, we can write to it, using the statement. Putc(ch,fptr) The putc() function is similar to the putch() and putchar() function. While putc() writes to a file. The file whose FILE structure is pointed to by the variable fptr, which we obtained then we open the file. This pointer has become our key to the file; we no longer refer to the file by name, but by the address stored in fptr. The writing process continues in the while loop; each time the putc() function is executed another character is written to the file.

Close the File Programming In C++, Lecture 10 By Umer Rana When we’ve finished writing to the file we need to close it, this is carried out with the statement fclose(fptr); Reading from a File If we can write to a file, we should be able to read from one using getch() function. The getc() function reads one character from the file till EOF (end of file). End-of-File It is not a character. It is actually an integer value, sent to the program by the operating system and defined in the stdio.h file to have a value of -1.

Write File On Disk By Character Programming In C++, Lecture 10 By Umer Rana #include int main() { char ch; FILE *fptr; fptr=fopen("out.txt","w"); while((ch=getche())!='\r') { putc(ch,fptr); } fclose(fptr); }

Read File On Disk By Character Programming In C++, Lecture 10 By Umer Rana #include int main() { char ch; FILE *fptr; fptr=fopen("out.txt","r"); while((ch=getc(fptr))!=EOF) { printf("%c",ch); } fclose(fptr); getch(); }

String (Line) Input Programming In C++, Lecture 10 By Umer Rana #include int main() { char string[81]; FILE *fptr; int a; fptr=fopen("out.txt","w"); while(strlen (gets(string) ) >0) { fputs(string,fptr); fputs("\n",fptr); } fclose(fptr); }

String (Line) Output Programming In C++, Lecture 10 By Umer Rana int main() { char string[81]; FILE *fptr; int a; fptr= fopen("out.txt","r"); fgets(String stored, Length of String, Point to the File) while(fgets(string,80,fptr)!=NULL) { printf("%s",string); } fclose(fptr); getch(); }

Formatted Input/output Programming In C++, Lecture 10 By Umer Rana similar to scanf() and printf() in addition provide file-pointer is include as the first argument. Example: fprintf(f1, “%d %f\n”, i, f); fprintf(stdout, “%f \n”, f); fscanf(f2, “%d %f”, &i, &f); fscanf returns EOF when end-of-file reached fscanf() & fprintf()

Formatted Input Programming In C++, Lecture 10 By Umer Rana #include int main() { char name[40]; int code; float height; FILE *fptr; fptr=fopen("out.txt","w"); do{ printf("Type Name, code number & height:"); scanf("%s %d %f",name,&code,&height); fprintf(fptr,"%s %d %f \n",name,code,height); }while(strlen(name)>2); fclose(fptr); }

Formatted Output Programming In C++, Lecture 10 By Umer Rana int main() { char name[40]; int code; float height; FILE *fptr; fptr=fopen("out.txt","r"); while( fscanf( fptr,"%s %d %f",name,&code,&height)!=EOF) printf("%s %d %f \n",name,code,height); fclose(fptr); getch(); }

Record Input / Output Programming In C++, Lecture 10 By Umer Rana Record some time called block I/O, Record write numbers to the disk files in binary. Records I/O also permits writing any amount of data at once. Process is not limited to a single character or string or to the few values that can be placed in a fprintf() or fscanf() functions. Arrays, structures, structures of arrays, and other data constructions can be written with a single statement. Record Input / Output functions are fwrite() fread()

Record Input (fwirte()) Programming In C++, Lecture 10 By Umer Rana int main() {struct { char name[40]; int agnumb; double height; }agent; char numstr[40]; FILE *fptr; if((fptr=fopen("agent.rec","wb"))==NULL) printf("Cannot open file agents.rec"); do { printf("\n Enter Name:"); scanf("%s",&agent.name); printf("\n Enter Number:"); scanf("%d",&agent.agnumb); printf("\n Enter Height:"); scanf("%lf",&agent.height); fwrite(&agent,sizeof(agent),1,fptr); printf("Add another Agent (y/n)"); }while(getche()=='y'); fclose(fptr); }

Record Output (fread()) Programming In C++, Lecture 10 By Umer Rana int main() {struct { char name[40]; int agnumb; double height; }agent; char numstr[40]; FILE *fptr; if((fptr=fopen("agent.rec","rb"))==NULL) { printf("Cannot open file agents.rec"); } while (fread(&agent,sizeof(agent),1,fptr)==1) { printf("\nName: %s",agent.name); printf("\nAge: %d",agent.agnumb); printf("\nHeight: %.2lf",agent.height); } fclose(fptr); getch(); }

Random Access Programming In C++, Lecture 10 By Umer Rana Randomly Access means directly accessing a particular data items, even thought it may be in the middle of the file.

Random Access Programming In C++, Lecture 10 By Umer Rana int main() {struct { char name[40]; int agnumb; double height; }agent; FILE *fptr; int recno; long int offset; if((fptr=fopen("agent.rec","r"))==NULL) { printf("Cannot open file agents.rec"); getch(); exit(1); } printf("Enter Record number: "); scanf("%d",&recno); offset=(recno-1) * sizeof(agent); printf("\n%d",offset); getch(); if(fseek(fptr,offset,0)!=0) printf("Cannot Move Pointer there."); fread(&agent,sizeof(agent),1,fptr); printf("\nName: %s",agent.name); printf("\nAge: %d",agent.agnumb); printf("\nHeight:%.2lf",agent.height); fclose(fptr); getch(); }

File Pointer Programming In C++, Lecture 10 By Umer Rana A file pointer is a pointer to a particular byte in a file. fseek() fseek(Pointer to File, Position, Mode) Give control to move the pointer over the file. Offset This tell the number of bytes from a particulate place to start reading Mode This determine where the offset will be measured from. ModeOffset is Measured from 0Beginning of file 1Current position of file pointer 2End of file.

Errors that occur during I/O Programming In C++, Lecture 10 By Umer Rana Typical errors that occur – trying to read beyond end-of-file – trying to use a file that has not been opened – perform operation on file not permitted by ‘fopen’ mode – open file with invalid filename – write to write-protected file

Error Condition Programming In C++, Lecture 10 By Umer Rana ferror() This function takes one argument the file pointer. If it return a value of 0 (False) it mean there is no error. If it return a value of non Zero (True) it mean there is an error. if(ferror(fp) !=0) printf(“An error has occurred\n”); perror() It takes a string supplied by the program as an argument this string is usually an error message indicating where in the program the error occurred. Example See on Page 573