Programming In C Spring Semester 2013 Lecture 10

  • Slides: 24
Download presentation
Programming In C++ Spring Semester 2013 Lecture 10 Programming In C++, Lecture 10 By

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

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

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. a. b. c. d. 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 Character I/O String I/O getc( ) putc( ) Formatted I/O

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

Syntax The fopen ( ) function opens a file whose name is pointed to

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 File Type Meaning “r” Open an existing file for reading only

Possibilities File Modes File Type Meaning “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. Programming In C++, Lecture 10 By Umer Rana

Open a File • Before we can write a file to a disk, or

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

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

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> int main() {

Write File On Disk By Character #include <stdio. h> #include<conio. h> int main() { 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() {

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> int main() { char string[81]; FILE *fptr; int a;

String (Line) Input #include<string. h> 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); Programming In C++, Lecture 10 By Umer Rana

String (Line) Output int main() { char string[81]; FILE *fptr; int a; fptr= fopen("out.

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

Formatted Input/output fscanf() & fprintf() similar to scanf() and printf() in addition provide file-pointer is include as the first argument. Example: fprintf(f 1, “%d %fn”, i, f); fprintf(stdout, “%f n”, f); fscanf(f 2, “%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> int main() { char name[40]; int code; float height; FILE

Formatted Input #include<string. h> 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); } Programming In C++, Lecture 10 By Umer Rana

Formatted Output int main() { char name[40]; int code; float height; FILE *fptr; fptr=fopen("out.

Formatted Output 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(); Programming In C++, Lecture 10 By Umer Rana

Record Input / Output • Record some time called block I/O, Record write numbers

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 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 { } Record Input (fwirte()) char name[40]; int agnumb; double

int main() { struct { } Record Input (fwirte()) 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); Programming In C++, Lecture 10 By Umer Rana

int main() { struct { Record Output (fread()) char name[40]; int agnumb; double height;

int main() { struct { Record Output (fread()) 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("n. Name: %s", agent. name); printf("n. Age: %d", agent. agnumb); printf("n. Height: %. 2 lf", agent. height); } } fclose(fptr); getch(); Programming In C++, Lecture 10 By Umer Rana

Random Access • Randomly Access means directly accessing a particular data items, even thought

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 int main() { struct { }agent; char name[40]; int agnumb; double height;

Random Access int main() { struct { }agent; char name[40]; int agnumb; double height; FILE *fptr; int recno; long int offset; 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. "); if((fptr=fopen("agent. rec", "r"))==NULL) { printf("Cannot open file agents. rec"); getch(); exit(1); } } Programming In C++, Lecture 10 By Umer Rana fread(&agent, sizeof(agent), 1, fptr); printf("n. Name: %s", agent. name); printf("n. Age: %d", agent. agnumb); printf("n. Height: %. 2 lf", agent. height); fclose(fptr); getch();

File Pointer • A file pointer is a pointer to a particular byte in

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. Mode Offset is Measured from 0 Beginning of file 1 Current position of file pointer 2 End of file. Programming In C++, Lecture 10 By Umer Rana

Errors that occur during I/O • Typical errors that occur – trying to read

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

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 occurredn”); 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