Professional Documents
Culture Documents
Outline
11.1 Introduction
11.2 The Data Hierarchy
11.3 Files and Streams
11.4 Creating a Sequential Access File
11.5 Reading Data from a Sequential Access File
11.6 Updating Sequential Access Files
11.7 Random Access Files
11.8 Creating a Random Access File
11.9 Writing Data Randomly to a Random Access File
11.10 Reading Data Sequentially from a Random Access File
11.11 Case Study: A Transaction Processing Program
11.12 Input/Output of Objects
1 Bit
• FILE *ifp;
– Creates a file pointer called ifp.
• ifp = fopen(“Test.txt”, “w”);
– fopen returns a FILE pointer to file specified.
– Takes two arguments – name of the file and file open
mode.
– Returns NULL if open fails.
11
12
13
FILE *ifp;
ifp = fopen(“Test.txt”,”r”);
if (ifp == NULL) {
printf(“Couldn’t open file!!”);
exit(1);
}
14
FILE *ifp;
ifp = fopen(“Test.txt”,”r”);
fscanf(ifp,”%d %d”, &a,&b);
• the fscanf function would read values from the
file "pointed" to by ifp and assign those values
to a and b.
• If the file pointer is stdin then fscanf behaves
like scanf
15
18
int a = 5, b = 20;
FILE *wfp ;
wfp = fopen( "results", "w" );
fprintf(wfp,"%d%d\n",a,b);
• fprintf function would write the values stored in
a and b to the file "pointed" to by wfp.
19
20
21
• Opening a file
• Reading data from a file
• Writing data to a file
• Closing a file
22
23
#include<stdio.h>
#include<stdlib.h>
int main(void) {
char str[] = "Hello World!!";
char str1[15];
FILE *ifp, *ofp;
if ((ifp=fopen("Test.txt","w")) == NULL)
exit(1);
fprintf(ifp, "%s\n", str);
fclose(ifp);
if ((ofp=fopen("Test.txt","r")) == NULL)
exit(1);
fscanf(ofp, "%s", str1);
printf("%s", str1);
fclose(ofp); 24
}
2000 Prentice Hall, Inc.
All rights reserved.
1 /* Fig. 11.3: fig11_03.c
2 Create a sequential file */ Outline
3 #include <stdio.h>
4
5 int main() 1. Initialize variables
6 { and FILE pointer
7 int account;
8 char name[ 30 ];
1.1 Link the pointer to
9 double balance;
a file
10 FILE *cfPtr; /* cfPtr = clients.dat file pointer */
11
12 if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) 2. Input data
13 printf( "File could not be opened\n" );
14 else {
15 printf( "Enter the account, name, and balance.\n" ); 2.1 Write to file
16 printf( "Enter EOF to end input.\n" ); (fprintf)
17 printf( "? " );
18 scanf( "%d%s%lf", &account, name, &balance );
19
3. Close file
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; 2000 Prentice Hall, Inc.
31 } All rights reserved.
Enter the account, name, and balance.
Enter EOF to end input.
Outline
? 100 Jones 24.98
? 200 Doe 345.67
? 300 White 0.00
Program Output
? 400 Stone -42.16
? 500 Rich 224.62
?
? 2
? 3
}byte offsets
}
}
}
}
}
100
bytes
100
bytes
100
bytes
100
bytes
100
bytes }100
bytes
2000 Prentice Hall, Inc.
All rights reserved.
11.7 Creating a Random Access File
• Data in random access files
– Unformatted (stored as "raw bytes")
• All data of the same type (ints, for example) uses the same
amount of memory
• All records of the same type have a fixed length
• Data not human readable
39
162 }
163 }
164
166 {
168