View
43
Download
2
Category
Tags:
Preview:
DESCRIPTION
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 - PowerPoint PPT Presentation
Citation preview
1
CSC 211Data Structures
Lecture 32
Dr. Iftikhar Azim Niazianiaz@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); fp is 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()
fputc Writes 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
OutlineOutline
24
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
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 }
OutlineOutline
25
Program Output
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?
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 pointerfscanf( 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)
OutlineOutline
27
1. Initialize variables
1.1 Link pointer to file
2. Read data (fscanf)
2.1 Print
3. Close file
Program Output
1 /* Fig. 11.7: fig11_07.c2 Reading and printing a sequential file */3 #include <stdio.h>45 int main()6 { 7 int account;8 char name[ 30 ];9 double balance;10 FILE *cfPtr; /* cfPtr = clients.dat file pointer */1112 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 );1718 while ( !feof( cfPtr ) ) { 19 printf( "%-10d%-13s%7.2f\n", account, name, balance );20 fscanf( cfPtr, "%d%s%lf", &account, name, &balance );21 }2223 fclose( cfPtr );24 }2526 return 0;27 }Account Name Balance100 Jones 24.98200 Doe 345.67300 White 0.00400 Stone -42.16500 Rich 224.62
OutlineOutline
28
1. Initialize variables
2. Open file
2.1 Input choice
2.2 Scan files
3. Print
1 /* Fig. 11.8: fig11_08.c2 Credit inquiry program */3 #include <stdio.h>45 int main()6 { 7 int request, account;8 double balance;9 char name[ 30 ];10 FILE *cfPtr;1112 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 );2122 while ( request != 4 ) { 23 fscanf( cfPtr, "%d%s%lf", &account, name, 24 &balance );2526 switch ( request ) { 27 case 1:28 printf( "\nAccounts with zero "29 "balances:\n" );3031 while ( !feof( cfPtr ) ) { 32
OutlineOutline
29
2.2 Scan files
3. Print
33 if ( balance == 0 )34 printf( "%-10d%-13s%7.2f\n", 35 account, name, balance );3637 fscanf( cfPtr, "%d%s%lf", 38 &account, name, &balance );39 }4041 break;42 case 2:43 printf( "\nAccounts with credit "44 "balances:\n" );4546 while ( !feof( cfPtr ) ) { 4748 if ( balance < 0 )49 printf( "%-10d%-13s%7.2f\n", 50 account, name, balance );5152 fscanf( cfPtr, "%d%s%lf", 53 &account, name, &balance );54 }5556 break;57 case 3:58 printf( "\nAccounts with debit "59 "balances:\n" );6061 while ( !feof( cfPtr ) ) { 6263 if ( balance > 0 )64 printf( "%-10d%-13s%7.2f\n",
OutlineOutline
30
3.1 Close file
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 }
OutlineOutline
31
Program Output
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.98200 Doe 345.67500 Rich 224.62? 4End of run.
32
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
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
300 Worthington 0.00ones 32.87
300 Worthington 0.00
Data gets overwritten
33
Read and Write for Binary Filessize_t fread(void *buffer, size_t numbytes, size_t count, FILE *a_file);
size_t fwrite(void *buffer, size_t numbytes, size_t count, FILE *a_file);
Buffer in fread is 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 count is 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 int cat_num; float cost; }; typedef struct 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 – 4Lecture Topics
1Course Outline, Programming and problem solving, Software development method, Program control structures, Algorithm, Pseudocode and Flow chart
2 System Development Life cycle and its phases,Program Development Life cycle and its phases
3Generation of programming languages, Compiler Interpreter, Procedural and modular programmingStructure of a C Program, Data and data structure, Abstract Data Type (ADT)
4Data types in C Language, Arrays, Declaration, operations, Array and functions, Pointers, Arrays and pointers
38
Revision – Lectures 5 – 8Lecture Topics
5Concept of Pointer, Pointer operators, Address and Indirection, Pointer Arithmetic, Pointer and functionsPass by Value, Pass by Reference
6 Dynamic Memory Management with Pointers, Structures, Unions, Strings, Multidimensional Arrays
7Need for Data Structures, Selecting a data structureData structure philosophy, Classification, Common Operations, Arrays and Lists, List Operations
8Algorithm Analysis, Time and Space Complexity,Complexity of Algorithms, Measuring EfficiencyBig O Notation, Standard Analysis Techniques
39
Revision – Lecture 9 – 12Lecture Topics
9Algorithms and Complexity, Criteria for Algorithm Analysis, Complexity Analysis, Various Complexity Functions, Properties of Big O Notation
10 Data structure Operations, Array-based and Pointer based List ADT, Linked List, Linked List Operations
11Dynamic Representation, Allocation from Dynamic Storage, Returning unused storage back to dynamic storage, Linked List insert and delete Operations
12Cursor-based Implementation of List, Search Operation, Sequential Search, Concept, Algorithm and Implementation, Complexity of Sequential Search
40
Revision – Lectures 13 – 16Lecture Topics
13Binary Search, Concept, Algorithm and implementation, Binary search complexity, Searching Unordered and Ordered Linked Lists
14Sorting, Concept, Terminology, Classification, Stability of Key, Bubble Sort, Concept, Algorithm and implementation
15Bubble Sort Complexity, Selection Sort, Concept, Algorithm, Implementation, Complexity, Insertion Sort, Concept, Algorithm, Implementation, Complexity
16 Comparison of Bubble, Selection and Insertion Sort, Recursion, Concept, Example and Implementation
41
Revision – Lectures 17 – 20Lecture Topics
17Recursive Search Algorithms, Recursion with Linked Lists, Recursion and Iteration, Analysis of Recursion
18 Merge Sort, Concept, Algorithm and ImplementationComplexity of Merge Sort
19 Quick Sort, Concept, Algorithm and ImplementationComplexity of Quick Sort
20Comparison of Merge and Quick Sort, Shell Sort, Radix sort, Bucket Sort, Sorting techniques comparison
42
Revision – Lectures 21 – 24
Lecture Topics
21Doubly Linked List, Operations, Algorithm and Implementation Code, Doubly Linked List with Two Pointers
22 Queues, Operations, Algorithm and Implementation,Circular Queue and Deque operations
23 Stacks, Operations, Algorithm and Implementation,Stack Applications
24Trees, Concept, Examples and Applications, Tree Terminology, Types of Trees, General Trees, Representation and Traversal, Binary Tree
43
Revision – Lectures 25 – 28Lecture Topics
25 Binary Tree Operations and Traversal, Binary Search tree and its Operations
26Complete Binary Tree, Heaps, Heap OperationsApplications of Heaps, Priority Queue, Heap SortConcept , Algorithm and Implementation, Complexity, Comparison with Quick and Merge Sort
27Types of Binary trees, Expression Tree, Threaded Binary Tree, AVL Tree, Red-Black, Splay, Insertion and Deletion Operations, Time Complexity, B–trees
28Graphs, Terminology and Representation of GraphsOperations on Graphs, Graph Traversals, Breadth First Search (BFS), Depth First Search (DFS)
44
Revision – Lectures 29 – 32Lecture Topics
29Shortest Path Problem, Dijkstra’s Algorithm, Bellman Ford Algorithm, Spanning Tree, Minimum Spanning Tree, Kruskal and Prim Algorithm
30Dictionaries, Table, Concept, Operations and Implementation, Hash Table, Hashing and Hash Function, Hash Tables Implementation, Applications
31Hash Function, Properties of a Good Hash FunctionHash Function Methods, File, Text and Binary FilesOperations on Files, File Access Methods, Sequential Files, Indexed Files, Hashed Files
32File operations implementation in C, File operations on Text and Sequential Binary Files, Revision
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
Recommended