Download presentation
Presentation is loading. Please wait.
1
EPSII 59:006 Spring 2004
2
Makeup Exam Tuesday, April 13, 7:00 PM Room TBD Same procedure
You must your instructor with: Class number of conflict Instructors name Time of class
3
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)
4
Initialize variables Link pointer to file Read data (fscanf) Print
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 ) printf( "File could not be opened\n" ); 14 else { printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" ); fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 17 while ( !feof( cfPtr ) ) { printf( "%-10d%-13s%7.2f\n", account, name, balance ); fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); } 22 fclose( cfPtr ); 24 } 25 26 return 0; 27 } Initialize variables Link pointer to file Read data (fscanf) Print Close file Program Output Account Name Balance Jones Doe White Stone Rich
5
Initialize variables Open file Input choice Scan files Print
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 ) printf( "File could not be opened\n" ); 14 else { printf( "Enter request\n" " 1 - List accounts with zero balances\n" " 2 - List accounts with credit balances\n" " 3 - List accounts with debit balances\n" " 4 - End of run\n? " ); scanf( "%d", &request ); 21 while ( request != 4 ) { fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); 25 switch ( request ) { case 1: printf( "\nAccounts with zero " "balances:\n" ); 30 while ( !feof( cfPtr ) ) { 32 Initialize variables Open file Input choice Scan files Print
6
Scan files Print 33 if ( balance == 0 )
printf( "%-10d%-13s%7.2f\n", account, name, balance ); 36 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); } 40 break; case 2: printf( "\nAccounts with credit " "balances:\n" ); 45 while ( !feof( cfPtr ) ) { 47 if ( balance < 0 ) printf( "%-10d%-13s%7.2f\n", account, name, balance ); 51 fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); } 55 break; case 3: printf( "\nAccounts with debit " "balances:\n" ); 60 while ( !feof( cfPtr ) ) { 62 if ( balance > 0 ) printf( "%-10d%-13s%7.2f\n", Scan files Print
7
Close file 65 account, name, balance ); 66
fscanf( cfPtr, "%d%s%lf", &account, name, &balance ); } 70 break; } 73 rewind( cfPtr ); printf( "\n? " ); scanf( "%d", &request ); } 78 printf( "End of run.\n" ); fclose( cfPtr ); 81 } 82 83 return 0; 84 } Close file
8
Program Output Enter request 1 - List accounts with zero balances
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: White ? 2 Accounts with credit balances: Stone ? 3 Accounts with debit balances: Jones Doe Rich ? 4 End of run. Program Output
9
Reading Data from a 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 Jones (old data in file) If we want to change White's name to Worthington, 300 White Jones 32.87 300 Worthington 0.00ones 32.87 300 Worthington 0.00 Data gets overwritten
10
Sequential File I/O Example
Revisit substring generation and dictionary comparison Previously: %cat dict_file_name | ./a.out stringGoesHere Or %./a.out < dict_file_name With file I/O, we can now Open dict_file_name and read words Write words we find to a file %a.out dict_file_name stringGoesHere
11
/* read in 2 strings from the command line --
1) dictionary file 2) string to search for and try all contiguous permutations against dictionary */ #define NUM_WORDS 45427 #define MAX 30 // max word length #include<stdio.h> // prototypes void get_chars(char *source, char *dest, int start, int len); main(int argc, char* argv[]) { char words[NUM_WORDS][MAX]; char temp[MAX]; int i = 0, j = 0, length = 0; FILE *dict, *output; if(argc < 3) { printf("Useage: a.out dict_file string\n"); exit(1); } if((dict = (fopen(argv[1],"r"))) == NULL) { printf("Failed to open dictionary file %s\n",argv[1]); else { printf("Reading words from %s\n",argv[1]); while(i<NUM_WORDS) { fscanf(dict,"%s",words[i]); printf("Reading word %d\r",i); // for debugging // printf("word = %s %d\n",words[i],i); i++;
12
if((output = (fopen("results.txt","w"))) == NULL) {
printf("Failed to open results.txt\n"); exit(1); } else { printf("Writing results.txt\n"); for(length=1;length<=strlen(argv[2]);length++) { for(i=0;i<=strlen(argv[2])-length;i++) get_chars(argv[2],temp,i,length); //printf("temp = %s\n",temp); for(j=0;j<NUM_WORDS;j++) { //printf("%s %s\n",temp,words[j]); if(strcmp(temp,words[j])==0) fprintf(output,"%s\n",temp); } void get_chars(char *source, char *dest, int start, int len){ int i = 0; int dstart = 0; for(i=start,dstart=0;dstart<len;i++,dstart++) dest[dstart] = source[i]; dest[dstart]='\0';
13
Illustration of Structures
Structures are datatypes (“things”) just like an “integer” is a datatype Next 2 slides show 1) simple program with function (integer pass-by-value) 2) simple program with function (integer replaced with “struct record” -- still pass-by-value) Structures may be nested
14
// prototype int increment(int i); main() { int x = 5; int result = 0; result = increment(x); printf("result = %d\n",result); } // Function int increment(int i) { i = i + 1; return(i);
15
struct record { int a; float b; }; // prototype //int increment(int i); -- old prototype struct record increment(struct record i); main() { //int x = 5; struct record x = {5,5.5}; // same thing x.a = 5; x.b = 5.5; //int result = 0; struct record result; result = increment(x); printf("result = %d\n",result.a); } // End main // Function struct record increment(struct record i) { // i = i + 1; i.a = i.a + 1; return(i); }
16
Nested Structures Output: % ./a.out First = Homer
#include<stdio.h> struct FLnames { char first[10]; char last[10]; }; struct record { int id; // just a plain old int struct FLnames name; char city[20]; main() { struct record employee; employee.id = 18; strcpy(employee.name.first,"Homer"); strcpy(employee.name.last,"Simpson"); strcpy(employee.city,"Iowa City"); printf("first = %s\n",employee.name.first); } Output: % ./a.out First = Homer
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.