1 / 45

CSC 211 Data Structures Lecture 32

CSC 211 Data Structures Lecture 32. Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk. 1. Last Lecture Summary. Hash Function Properties of a Good Hash Function Hash Function Methods File Text and Binary Files Operations on Files File Access Methods Sequential Files Indexed Files

oakley
Download Presentation

CSC 211 Data Structures Lecture 32

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. CSC 211Data StructuresLecture 32 Dr. Iftikhar Azim Niaz ianiaz@comsats.edu.pk 1

  2. Last Lecture Summary • Hash Function • Properties of a Good Hash Function • Hash Function Methods • File • Text and Binary Files • Operations on Files • File Access Methods • Sequential Files • Indexed Files • Hashed Files 2

  3. Objectives Overview • File Implementation in C Language • Basic File Operations • Opening a file • Reading data from a file • Writing data to a file • Closing a file • File operations on Text Files • File operations on Sequential Binary Files • Revision of the Course • Lecture 1 to Lecture 31

  4. Files - Implementation • Files are places where data can be stored permanently. • Some programs expect the same set of data to be fed as input every time it is run. • Cumbersome. • Better if the data are kept in a file, and the program reads from the file. • Programs generating large volumes of output. • Difficult to view on the screen. • Better to store them in a file for later viewing/ processing 4

  5. Text Data Files • When you use a file to store data for use by a program, that file usually consists of text (alphanumeric data) and is therefore called a text file. • Can be created, updated, and processed by C programs • Are used for permanent storage of large amounts of data • Storage of data in variables and arrays is only temporary

  6. Basic Files Operations • Opening a file • Reading data from a file • Writing data to a file • Closing a file

  7. Opening a File • A file must be “opened” before it can be used. FILE *fp; : fp = fopen (filename, mode); • fpis declared as a pointer to the data type FILE. • filename is a string - specifies the name of the file. • fopen returns a pointer to the file which is used in all subsequent file operations. • mode is a string which specifies the purpose of opening the file: “r” :: open the file for reading only “w” :: open the file for writing only “a” :: open the file for appending data to it

  8. File Modes • r - open a file in read-mode, set the pointer to the beginning of the file. • w - open a file in write-mode, set the pointer to the beginning of the file. • a- open a file in write-mode, set the pointer to the end of the file. • rb- open a binary-file in read-mode, set the pointer to beginning of file. • wb - open a binary-file in write-mode, set the pointer to beginning of file. • ab - open a binary-file in write-mode, set the pointer to the end of the file. • r+ - open a file in read/write-mode, if file does not exist, it will not be created. • w+- open a file in read/write-mode, set the pointer to the beginning of file. • a+ - open a file in read/append mode. • r+b - open a binary-file in read/write-mode, if the file does not exist, it will not be created. • w+b - open a binary-file in read/write-mode, set pointer to beginning of file. • a+b- open a binary-file in read/append mode.

  9. File Modes • Points to note: • Several files may be opened at the same time. • For the “w” and “a” modes, if the named file does not exist, it is automatically created. • For the “w” mode, if the named file exists, its contents will be overwritten.

  10. Opening a File FILE *in, *out ; in = fopen (“mydata.dat”, “r”) ; out = fopen (“result.dat”, “w”); FILE *empl ; char filename[25]; scanf (“%s”, filename); empl = fopen (filename, “r”) ;

  11. Closing a File • After all operations on a file have been completed, it must be closed. • Ensures that all file data stored in memory buffers are properly written to the file. • General format: fclose (file_pointer) ; FILE *xyz ; xyz = fopen (“test.txt”, “w”) ; ……. fclose (xyz) ;

  12. Closing a File • fclose(FILE pointer) • Closes specified file • Performed automatically when program ends • Good practice to close files explicitly • system resources are freed. • Also, you might not find that all the information that you've written to the file has actually been written to disk until the file is closed. • feof(FILE pointer) • Returns true if end-of-file indicator (no more data to process) is set for the specified file

  13. Read/Write Operations on Text Files • The simplest file input-output (I/O) function are getc and putc. • getc is used to read a character from a file and return it. char ch; FILE *fp; ch = getc (fp) ; • getc will return an end-of-file marker EOF, when the end of the file has been reached. • putc is used to write a character to a file. char ch; FILE *fp; putc (ch, fp) ;

  14. Text File - Example • Convert a text file to all UPPERCASE main() { FILE *in, *out ; char c ; in = fopen (“infile.dat”, “r”) ; out = fopen (“outfile.dat”, “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; }

  15. Read/Write Operations on Text Files • We can also use the file versions of scanf and printf, called fscanf and fprintf. • General format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; • Examples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “\nThe result is: %d”, xyz) ; fprintf • Used to print to a file • It is like printf, except first argument is a FILE pointer (pointer to the file you want to print in)

  16. Some Points • How to check EOF condition when using fscanf? • Use the function feof if (feof (fp)) printf (“\n Reached end of file”) ; • How to check successful open? • For opening in “r” mode, the file must exist. if (fp == NULL) printf (“\n Unable to open file”) ;

  17. Example : Merge Two Text Files #include <stdio.h> int main() { FILE *fileA, /* first input file */ *fileB, /* second input file */ *fileC; /* output file to be created */ int num1, /* number to be read from first file */ num2; /* number to be read from second file*/ int f1, f2; /* Open files for processing */ fileA = fopen("class1.txt","r"); fileB = fopen("class2.txt","r"); fileC = fopen("class.txt","w");

  18. Example : Merge Two Files /* As long as there are numbers in both files, read and compare numbersone by one. Write the smaller number to the output file and read the next number in the file from which the smaller number is read. */f1 = fscanf(fileA, "%d", &num1); f2 = fscanf(fileB, "%d", &num2); while ((f1!=EOF) && (f2!=EOF)){ if (num1 < num2){ fprintf(fileC,"%d\n", num1); f1 = fscanf(fileA, "%d", &num1); } else if (num2 < num1) { fprintf(fileC,"%d\n", num2); f2 = fscanf(fileB, "%d", &num2); } else { /* numbs are equal:read from both files */fprintf(fileC,"%d\n", num1); f1 = fscanf(fileA, "%d", &num1); f2 = fscanf(fileB, "%d", &num2); } }

  19. Example : Merge Two Files • /* if reached end of second file, read the remaining numbers • from first file and write to output file */ while (f1!=EOF){fprintf(fileC,"%d\n", num1); f1 = fscanf(fileA, "%d", &num1); } • /* if reached the end of first file, read the remaining numbers • from second file and write to output file */ while (f2!=EOF){fprintf(fileC,"%d\n", num2); f2 = fscanf(fileB, "%d", &num2); } /* close files */fclose(fileA);fclose(fileB);fclose(fileC); return 0;} /* end of main */

  20. Files and Streams • C views each file as a sequence of bytes • File ends with the end-of-file marker • Stream created when a file is opened • Provide communication channel between files and programs • Opening a file returns a pointer to a FILE structure • Example file pointers: • stdin - standard input (keyboard) • stdout - standard output (screen) • stderr - standard error (screen) • FILE structure • File descriptor • Index into operating system array called the open file table • File Control Block (FCB) • Found in every array element, system uses it to administer the file

  21. Files and Streams • Read/Write functions in standard library • fgetc Reads one character from a file • Takes a FILE pointer as an argument • fgetc(stdin) equivalent to getchar() • fputcWrites one character to a file • Takes a FILE pointer and a character to write as an argument • fputc('a', stdout) equivalent to putchar('a') • fgets reads a line (string) from a file • fputs writes a line (string) to a file • fscanf / fprintf • File processing equivalents of scanf and printf

  22. Creating a Sequential Access File • C imposes no file structure • No notion of records in a file • Programmer must provide file structure • Creating a File • FILE *myPtr; • Creates a FILE pointer called myPtr • myPtr = fopen("myFile.dat", openmode); • Function fopen returns a FILE pointer to file specified • Takes two arguments – file to open and file open mode • If open fails, NULL returned • fprintf • Used to print to a file • Like printf, except first argument is a FILE pointer (pointer to the file you want to print in)

  23. Creating a Sequential Access File • feof(FILE pointer) • Returns true if end-of-file indicator (no more data to process) is set for the specified file • fclose(FILE pointer) • Closes specified file • Performed automatically when program ends • Good practice to close files explicitly • Details • Programs may process no files, one file, or many files • Each file must have a unique name and should have its own pointer

  24. 1 /* Fig. 11.3: fig11_03.c 2 Create a sequential file */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int account; 8 char name[ 30 ]; 9 double balance; 10 FILE *cfPtr; /* cfPtr = clients.dat file pointer */ 11 12 if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) 13 printf( "File could not be opened\n" ); 14 else { 15 printf( "Enter the account, name, and balance.\n" ); 16 printf( "Enter EOF to end input.\n" ); 17 printf( "? " ); 18 scanf( "%d%s%lf", &account, name, &balance ); 19 20 while ( !feof( stdin ) ) { 21 fprintf( cfPtr, "%d %s %.2f\n", 22 account, name, balance ); 23 printf( "? " ); 24 scanf( "%d%s%lf", &account, name, &balance ); 25 } 26 27 fclose( cfPtr ); 28 } 29 30 return 0; 31 } 1. Initialize variables and FILE pointer 1.1 Link the pointer to a file 2. Input data 2.1 Write to file (fprintf) 3. Close file

  25. Enter the account, name, and balance. Enter EOF to end input. ? 100 Jones 24.98 ? 200 Doe 345.67 ? 300 White 0.00 ? 400 Stone -42.16 ? 500 Rich 224.62 ? Program Output

  26. Reading Data from a Sequential Access File • Reading a sequential access file • Create a FILE pointer, link it to the file to read myPtr = fopen( "myFile.dat", "r" ); • Use fscanf to read from the file • Like scanf, except first argument is a FILE pointer fscanf( myPtr, "%d%s%f", &myInt, &myString, &myFloat ); • Data read from beginning to end • File position pointer • Indicates number of next byte to be read / written • Not really a pointer, but an integer value (specifies byte location) • Also called byte offset • rewind( myPtr ) • Repositions file position pointer to beginning of file (byte 0)

  27. 1 /* Fig. 11.7: fig11_07.c 2 Reading and printing a sequential file */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int account; 8 char name[ 30 ]; 9 double balance; 10 FILE *cfPtr; /* cfPtr = clients.dat file pointer */ 11 12 if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) 13 printf( "File could not be opened\n" ); 14 else { 15 printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); 16 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 17 18 while ( !feof( cfPtr ) ) { 19 printf( "%-10d%-13s%7.2f\n", account, name, balance ); 20 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 21 } 22 23 fclose( cfPtr ); 24 } 25 26 return 0; 27 } 1. Initialize variables 1.1 Link pointer to file 2. Read data (fscanf) 2.1 Print 3. Close file Program Output Account Name Balance 100 Jones 24.98 200 Doe 345.67 300 White 0.00 400 Stone -42.16 500 Rich 224.62

  28. 1 /* Fig. 11.8: fig11_08.c 2 Credit inquiry program */ 3 #include <stdio.h> 4 5 int main() 6 { 7 int request, account; 8 double balance; 9 char name[ 30 ]; 10 FILE *cfPtr; 11 12 if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) 13 printf( "File could not be opened\n" ); 14 else { 15 printf( "Enter request\n" 16 " 1 - List accounts with zero balances\n" 17 " 2 - List accounts with credit balances\n" 18 " 3 - List accounts with debit balances\n" 19 " 4 - End of run\n? " ); 20 scanf( "%d", &request ); 21 22 while ( request != 4 ) { 23 fscanf( cfPtr, "%d%s%lf", &account, name, 24 &balance ); 25 26 switch ( request ) { 27 case 1: 28 printf( "\nAccounts with zero " 29 "balances:\n" ); 30 31 while ( !feof( cfPtr ) ) { 32 1. Initialize variables 2. Open file 2.1 Input choice 2.2 Scan files 3. Print

  29. 33 if ( balance == 0 ) 34 printf( "%-10d%-13s%7.2f\n", 35 account, name, balance ); 36 37 fscanf( cfPtr, "%d%s%lf", 38 &account, name, &balance ); 39 } 40 41 break; 42 case 2: 43 printf( "\nAccounts with credit " 44 "balances:\n" ); 45 46 while ( !feof( cfPtr ) ) { 47 48 if ( balance < 0 ) 49 printf( "%-10d%-13s%7.2f\n", 50 account, name, balance ); 51 52 fscanf( cfPtr, "%d%s%lf", 53 &account, name, &balance ); 54 } 55 56 break; 57 case 3: 58 printf( "\nAccounts with debit " 59 "balances:\n" ); 60 61 while ( !feof( cfPtr ) ) { 62 63 if ( balance > 0 ) 64 printf( "%-10d%-13s%7.2f\n", 2.2 Scan files 3. Print

  30. 65 account, name, balance ); 66 67 fscanf( cfPtr, "%d%s%lf", 68 &account, name, &balance ); 69 } 70 71 break; 72 } 73 74 rewind( cfPtr ); 75 printf( "\n? " ); 76 scanf( "%d", &request ); 77 } 78 79 printf( "End of run.\n" ); 80 fclose( cfPtr ); 81 } 82 83 return 0; 84 } 3.1 Close file

  31. Enter request 1 - List accounts with zero balances 2 - List accounts with credit balances 3 - List accounts with debit balances 4 - End of run ? 1 Accounts with zero balances: 300 White 0.00 ? 2 Accounts with credit balances: 400 Stone -42.16 ? 3 Accounts with debit balances: 100 Jones 24.98 200 Doe 345.67 500 Rich 224.62 ? 4 End of run. Program Output

  32. 300 White 0.00 400 Jones 32.87(old data in file) If we want to change White's name to Worthington, 300 White 0.00 400 Jones 32.87 Data gets overwritten 300 Worthington 0.00ones 32.87 300 Worthington 0.00 Reading Data from a Sequential Access File • Sequential access file • Cannot be modified without the risk of destroying other data • Fields can vary in size • Different representation in files and screen than internal representation • 1, 34, -890 are all ints, but have different sizes on disk

  33. Read and Write for Binary Files size_tfread(void *buffer, size_tnumbytes, size_tcount, FILE *a_file); size_tfwrite(void *buffer, size_tnumbytes, size_tcount, FILE *a_file); • Buffer in freadis a pointer to a region of memory that will receive the data from the file. Buffer in fwrite() is a pointer to the information that will be written to the file. • The second argument is the size of the element; it is in bytes. • Size_t is an unsigned integer. • For example, if you have an array of characters, you would want to read it in one byte chunks, so numbytes is one. You can use the sizeof operator to get the size of the various datatypes; for example, if you have a variable, int x; you can get the size of x with sizeof(x);

  34. Read and Write for Binary Files • The third argument countis simply how many elements you want to read or write; for example, if you pass a 100 element array • The final argument is simply the file pointer • fread() returns number of items read and • fwrite() returns number of items written • To check to ensure the end of file was reached, use the feof function, which accepts a FILE pointer and returns true if the end of the file has been reached.

  35. Sample Program - 1 /* a simple example of using fread and fwrite to read and write an array of structures */ #include <stdio.h> #include <conio.h> int main() { FILE *fp; // File pointer struct prod { // declaring record intcat_num; float cost; }; typedefstruct prod product; // type definition product a[3] = {{2,20.1},{4,40.1},{6,60.1}};// array of records product k, *p = &k;

  36. Sample Program - 2 • // opening the text file in read/write mode • fp = fopen("c:\fread1.dat","w+b"); • // write the entire array into the file pointed to by fp • fwrite(a, sizeof(product), 3, fp); • // prepare for reading from the beginning of the file • rewind(fp); • // read from the file one product at a time • for (i=0; i<3; i++) { • fread(p, sizeof(product), 1, fp); • printf(" product %d, cat_num=%d, cost=%f\n", • i, p->cat_num, p->cost); • } // end of for loop • getch(); • } // end of main program

  37. Revision – Lectures 1 – 4

  38. Revision – Lectures 5 – 8

  39. Revision – Lecture 9 – 12

  40. Revision – Lectures 13 – 16

  41. Revision – Lectures 17 – 20

  42. Revision – Lectures 21 – 24

  43. Revision – Lectures 25 – 28

  44. Revision – Lectures 29 – 32

  45. Summary • File Implementation in C Language • Basic File Operations • Opening a file • Reading data from a file • Writing data to a file • Closing a file • File operations on Text Files • File operations on Sequential Binary Files • Revision of the Course • Lecture 1 to Lecture 31

More Related