Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By Umer Rana."— Presentation transcript:

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

2 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.

3 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.

4 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( )

5 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);

6 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.

7 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.

8 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.

9 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.

10 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); }

11 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(); }

12 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); }

13 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(); }

14 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()

15 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); }

16 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(); }

17 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()

18 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); }

19 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(); }

20 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.

21 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(); }

22 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.

23 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

24 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


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

Similar presentations


Ads by Google