240 likes | 351 Views
Programming In C++. Spring Semester 2013 Lecture 10. Files. 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.
E N D
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana
Files 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. Programming In C++, Lecture 10 By Umer Rana
Standard & System I/O 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. Character I/O String I/O Formatted I/O 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. Programming In C++, Lecture 10 By Umer Rana
Categories of Disk I/O Programming In C++, Lecture 10 By Umer Rana
Syntax 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); Programming In C++, Lecture 10 By Umer Rana
Possibilities File Modes Programming In C++, Lecture 10 By Umer Rana
Open a File 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. Programming In C++, Lecture 10 By Umer Rana
Write to a File 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. Programming In C++, Lecture 10 By Umer Rana
Close the File 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. Programming In C++, Lecture 10 By Umer Rana
Write File On Disk By Character #include <stdio.h> #include<conio.h> intmain() { char ch; FILE *fptr; fptr=fopen("out.txt","w"); while((ch=getche())!='\r') { putc(ch,fptr); } fclose(fptr); } Programming In C++, Lecture 10 By Umer Rana
Read File On Disk By Character #include <stdio.h> #include<conio.h> int main() { char ch; FILE *fptr; fptr=fopen("out.txt","r"); while((ch=getc(fptr))!=EOF) { printf("%c",ch); } fclose(fptr); getch(); } Programming In C++, Lecture 10 By Umer Rana
String (Line) Input #include<string.h> intmain() { 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); } Programming In C++, Lecture 10 By Umer Rana
String (Line) Output 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(); } Programming In C++, Lecture 10 By Umer Rana
Formatted Input/output fscanf() & fprintf() 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 Programming In C++, Lecture 10 By Umer Rana
Formatted Input #include<string.h> intmain() { 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); } Programming In C++, Lecture 10 By Umer Rana
Formatted Output intmain() { 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(); } Programming In C++, Lecture 10 By Umer Rana
Record Input / Output • 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() Programming In C++, Lecture 10 By Umer Rana
int main() {struct { char name[40]; intagnumb; 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 Input (fwirte()) Programming In C++, Lecture 10 By Umer Rana
int main() { struct { char name[40]; intagnumb; 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(); } Record Output (fread()) Programming In C++, Lecture 10 By Umer Rana
Random Access • Randomly Access means directly accessing a particular data items, even thought it may be in the middle of the file. Programming In C++, Lecture 10 By Umer Rana
Random Access intmain() {struct { char name[40]; intagnumb; double height; }agent; FILE *fptr; intrecno; 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(); } Programming In C++, Lecture 10 By Umer Rana
File Pointer • 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. Programming In C++, Lecture 10 By Umer Rana
Errors that occur during I/O • 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 Programming In C++, Lecture 10 By Umer Rana
Error Condition • 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 Programming In C++, Lecture 10 By Umer Rana