1 / 41

240-222 Computer Programming Techniques Semester 1, 1998

240-222 Computer Programming Techniques Semester 1, 1998. 13. File Processing. Objectives of these slides: to introduce the file processing features of C for text and binary files. Overview:. 1. Opening a File 2. Reading and Writing 3. Example: Double Space a File 4. Random Access

annawade
Download Presentation

240-222 Computer Programming Techniques Semester 1, 1998

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 240-222 Computer Programming TechniquesSemester 1, 1998 13. File Processing Objectives of these slides: to introduce the file processing features of C for text and binary files

  2. Overview: 1. Opening a File 2. Reading and Writing 3. Example: Double Space a File 4. Random Access 5. Text Files and Binary Files Compared

  3. 1. Opening a File • Declare a file pointer: FILE *fp; • Assign to a the file pointer: fp = fopen("clients.dat", "w"); • Function prototype: FILE *fopen(char *name, char *mode);

  4. fopen() Modes Mode Meaning"r" open text file for reading"w" open text file for writing. If the file already exists, discard the contents."a" open text file for appending

  5. 2. Reading and Writing • of text files • reading and writing will be sequential 2.1. Writing 2.2. clients.dat 2.3. Reading

  6. 2.1. Writing Sec. 11.4 /* write sequentially to clients.dat */ #include <stdio.h>int main(){ int acc; char name[10]; float bal;FILE *cfptr; if ((cfptr = fopen("clients.dat","w")) == NULL) printf("File could not be opened\n"); else { : continued

  7. printf("Enter acc, name, & bal.\n? "); scanf("%d%s%f", &acc, name, &bal); while (!feof(stdin)) {fprintf(cfptr, "%d %s %.2f\n", acc, name, bal); printf("? "); scanf("%d%s%f", &acc, name, &bal); }fclose(cfptr); } return 0;}

  8. 2.2. clients.dat 1 Davison 23.672 Pohl 0.03356 Mason 89.0145 Jackson 11.00 :

  9. 2.3. Reading Sec. 11.5 /* read sequentially from clients.dat */#include <stdio.h>int main(){ int acc; char name[10]; float bal; FILE *cfptr; if((cfptr = fopen("clients.dat", "r")) == NULL) printf("File could not be opened\n"); else { : continued

  10. printf("Acc Name Bal\n");fscanf(cfptr, "%d %s %f", &acc, name, &bal); while(!feof(cfptr)) { printf("%d %s %f\n", acc,name,bal);fscanf(cfptr, "%d %s %f", &acc, name, &bal); } fclose(cfptr); } return 0;}

  11. 3. Example: Double Space a File • Call: $ dbl_space file1 file2 3.1. dbl_space.c 3.2. double_space() 3.3. prn_info() 3.4. A Graceful fopen()

  12. 3.1. dbl_space.c #include <stdio.h>#include <stdlib.h>void double_space(FILE *, FILE *);void prn_info(char *);int main(int argc, char *argv[]){ FILE *ifp, *ofp; if (argc != 3){ prn_info(argv[0]); exit(1); } : continued

  13. ifp = fopen(argv[1], "r"); /* read */ ofp = fopen(argv[2], "w"); /* write */ double_space(ifp, ofp); fclose(ifp); fclose(ofp); return 0;}

  14. 3.2. double_space() void double_space(FILE *ifp, FILE *ofp){ int c; while ((c = getc(ifp)) != EOF) {putc(c, ofp); if (c == ' ')putc(' ', ofp); /* found space -- double it */ }}

  15. 3.3. prn_info() void prn_info(char *pgn_name)/* version 1 */{ printf("\nUsage: %s infile outfile\n", pgn_name); } void prn_info(char *pgn_name)/* version 2 */{ fprintf(stderr, "\nUsage: %s inf outf\n", pgn_name);}

  16. 3.4. A Graceful fopen() FILE *gfopen(char *file_name, char *mode){ FILE *fp; if ((fp = fopen(file_name, mode)) == NULL) { fprintf(stderr, "\nCannot open %s\n", file_name); exit(1); } return fp;}

  17. 4. Random Access 4.1. Insertion Problem 4.2. How to do Random Access 4.3. Binary Files 4.4. fwrite() 4.5. Creation Program 4.6. fseek() 4.7. Manipulation Program 4.8. fread() 4.9. Printing Program

  18. 4.1. Insertion Problem • clients.dat: 0 White 12.5630 Davison 2345.7832 Deitel 0.00 : :1002 Pohl 0.0323 White 12.5135 Brown 1.45 • insertion using sequential read/write is very expensive

  19. 4.2. How to do Random Access Creation Program • Create a binary file called creditaccount.dat using fwrite() where each entry is a fixed length. acctnum lastname firstname bal 1 Davison Andrew 12.27 2 White Jim 3.24 3 Brown Mary 23.67 :: :: :: :: 100 Wai Loke Seng 4.45

  20. Manipulation Program • Use fseek() with acctnumto go to an entry immediately.

  21. 4.3. Binary Files • A binary file stores the system's internal representation of C data structures. • fopen() modes must include a "b": "rb", "wb", "ab", etc.

  22. 4.4. fwrite() • Informally: fwrite( <pointer to data being inserted>, <size of the data type>, <number of data items being inserted>, <file pointer>) • Example: fwrite(&blankclient, sizeof(struct clientdata), 1, cfptr);

  23. 4.5. Creation Program Fig. 11.11 /* create 100 empty clients in clientaccount.dat */#include <stdio.h>struct clientdata { int acctnum; char lastname[15]; /* size specified */ char firstname[10]; /* size specified */ float balance;}; : continued

  24. int main(){ int i; struct clientdata blankclient = {0, "", "", 0.0}; FILE *cfptr; if ((cfptr =fopen("creditaccount.dat", "wb")) == NULL) printf("File could not be opened.\n"); else { : continued

  25. for (i = 1; i <= 100; i++) fwrite( &blankclient, sizeof(struct clientdata), 1, cfptr); fclose (cfptr); } return 0;}

  26. 4.6. fseek() • Function prototype: int fseek(FILE *fp, long int offset, int whence); • offsetis the position of the entry. • In this case, it is: (acctnum of entry - 1) * size of an entry

  27. whenceis a symbolic constant indicating the starting position for a seek: SEEK_SET beginning of fileSEEK_CUR current location in fileSEEK_END end of file

  28. 4.7. Manipulation Program /* insert client info for a specified accnum into creditaccount.dat */#include <stdio.h>#include <string.h>struct clientdata { int acctnum; char last[15]; char first[10]; float bal;}; : continued

  29. int main(){ FILE *cfptr; struct clientdata client; if ((cfptr =fopen("creditaccount.dat", "rb+")) == NULL) printf("File could not be opened.\n"); else { : continued

  30. scanf("%d%s%s%f", &client.acctnum, client.last, client.first, &client.bal);fseek(cfptr, (client.acctnum - 1) * sizeof(struct clientdata), SEEK_SET); fwrite(&client, sizeof(struct clientdata), 1, cfptr); } fclose(cfptr); return 0;}

  31. 4.8. fread() • Informally: fread( <pointer to data structure for holding extracted data>, <size of the data type>, <number of data items>, <file pointer>) • Example: fread( &client, sizeof(struct clientdata), 1, cfptr);

  32. 4.9. Printing Program Fig.11.16 /* print out creditaccount.dat */#include <stdio.h>#include <string.h>struct clientdata { int acctnum; char last[15]; char first[10]; float bal;}; : continued

  33. int main(){ FILE *cfptr; struct clientdata client; if ((cfptr = fopen("creditaccount.dat", "rb")) == NULL) printf("File could not be opened.\n"); else { : continued

  34. printf("Acct Lastname Firstname Balance\n"); while (!feof(cfptr)) {fread(&client, sizeof(struct clientdata), 1, cfptr); if (strcmp(client.last, "") != 0) printf("%d %s %s %f\n", client.acctnum, client.last, client.first, client.bal); } } fclose(cfptr); return 0;}

  35. 5. Text Files and Binary Files Compared 5.1. Example Data Structures 5.2. Writing to a Text File 5.3. Writing to a Binary File 5.4. When to Use a Binary File 5.5. When to Use a Text File

  36. 5.1. Example Data Structures #define PNUM 50 /* number of planets */#define NAMELEN 20struct planet { char name[NAMELEN]; /* known size */ double diameter; double dist_sun;};struct planet earth;struct planet planets[PNUM];/* initialise earth and planets */

  37. 5.2. Writing to a Text File • Assume a text file is being accessed by the file pointer tp: fprintf(tp, "%s %lf %lf", earth.name, earth.diameter, earth.dist_sun); for (i = 0; i < PNUM; ++i) fprintf(tp, "%s %lf %lf", planets[i].name, planets[i].diameter, planets[i].dist_sun);

  38. 5.3. Writing to a Binary File • Assume a binary file is being accessed by the file pointer bp: fwrite(&earth, sizeof(planet), 1, bp); fwrite(planets, sizeof(planet)*PNUM, PNUM, bp);

  39. 5.4. When to Use a Binary File • If the file will contain complicated data structures. • If the file is to be manipulated using byte size-related functions (fwrite(), fseek(), etc).

  40. If speed of access is important(use fseek() instead of a sequential search). • If storage space is important (the data structure size can be defined).

  41. 5.5. When to Use a Text File • If the file will contain text. • If the file is to be moved between systems (text is portable).

More Related