EPSII 59:006 Spring 2004.

Slides:



Advertisements
Similar presentations
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Advertisements

© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Data files –Can be created, updated,
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Chapter 11 C File Processing Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Lec11: File Processing 廖雪花 TEL: 年 5 月.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating.
C Language Summary HTML version. I/O Data Types Expressions Functions Loops and Decisions Preprocessor Statements.
Files and Streams Python views files as sequential streams of bytes Each file ends with an end-of-file marker Opening a file creates an object associated.
CS1061 C Programming Lecture 18: Sequential File Processing A. O’Riordan, 2004, 2007 updated.
Console and File I/O - Basics Rudra Dutta CSC Spring 2007, Section 001.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 14 - Advanced C Topics Outline 14.1Introduction 14.2Redirecting Input/Output on UNIX and DOS Systems.
File Handling Spring 2013Programming and Data Structure1.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Practice Introduction Tree Operations. Binary Search Tree. File Processing Create, read, write and update files. Sequential.
1 File Processing Dr. Serdar ÇELEBİ. 2 Outline Introduction The Data Hierarchy Files and Streams Creating a Sequential Access File Reading Data from a.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
Programming Languages -1 (Introduction to C) files Instructor: M.Fatih AMASYALI
CSC 211 Data Structures Lecture 32
File IO and command line input CSE 2451 Rong Shi.
1 File Handling. 2 Storage seen so far All variables stored in memory Problem: the contents of memory are wiped out when the computer is powered off Example:
Chapter 11: Data Files and File Processing Files and streams Creating a sequential access file Reading data from a sequential access file Using fgetc()
Chapter 11 File Processing. Objectives In this chapter, you will learn: –To be able to create, read, write and update files. –To become familiar with.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Introduction Introduce some standard library functions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 11 – File Processing Outline 11.1Introduction.
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
C Program Design C File Processing 主講人:虞台文. Content Introduction Data Hierarchy Files and Streams Creating a Sequential-Access File Reading Data from.
Chapter 7 : File Processing1 File-Oriented Input & Output CHAPTER 7.
1 CHAPTER6 CHAPTER 6. Objectives: You’ll learn about;  Introduction  Files and streams  Creating a sequential access file  Reading data from a sequential.
24-2 Perform File I/O using file pointers FILE * data-type Opening and closing files Character Input and Output String Input and Output Related Chapter:
chap8 Chapter 12 Files (reference: Deitel ’ s chap 11)
C File Processing. Objectives To be able to create, write and update files. To become familiar with sequential access file processing. To become familiar.
GAME203 – C Files stdio.h C standard Input/Output “getchar()”
Chapter 12 Files (reference: Deitel ’ s chap 11) chap8.
File Access Storage of data in variables and arrays is temporary—such data is lost when a program terminates. Files are used for permanent retention of.
CSC141- Introduction to Computer Programming Teacher: AHMED MUMTAZ MUSTEHSAN Lecture – 29 Thanks for Lecture Slides:
File Processing Part 2. Random Access File In sequential access file, record in a file created with the formatted output function fprintf are not necessarily.
 2007 Pearson Education, Inc. All rights reserved. 1 C File Processing.
Files. FILE * u In C, we use a FILE * data type to access files. u FILE * is defined in /usr/include/stdio.h u An example: #include int main() { FILE.
1 Chapter 11 – File Processing Outline 11.1Introduction 11.2The Data Hierarchy 11.3Files and Streams 11.4Creating a Sequential Access File 11.5Reading.
Connecting to Files In order to read or write to a file, we need to make a connection to it. There are several functions for doing this. fopen() – makes.
C Programming Day 2. 2 Copyright © 2005, Infosys Technologies Ltd ER/CORP/CRS/LA07/003 Version No. 1.0 Union –mechanism to create user defined data types.
 2007 Pearson Education, Inc. All rights reserved C File Processing.
UniMAP SemI-11/12EKT120: Computer Programming1 Files.
Chapter 11 – File Processing
11 C File Processing.
File Processing Part 2.
Chapter 4 File Processing
TMF1414 Introduction to Programming
EKT120: Computer Programming
Day 02 Introduction to C.
File I/O.
File Processing (Cont.) and Structures
CS111 Computer Programming
Chapter 11 – File Processing
Chapter 14 - Advanced C Topics
File I/O in C Lecture 7 Narrator: Lecture 7: File I/O in C.
Programming and Data Structure
Text and Binary File Processing
File Input and Output.
File Handling.
Fundamental of Programming (C)
Pertemuan 14 File Processing II
File Handling in C.
EPSII 59:006 Spring 2004.
EPSII 59:006 Spring 2004.
Characters and Strings Functions
Chapter 11 Files chap8.
Files Chapter 8.
Presentation transcript:

EPSII 59:006 Spring 2004

Makeup Exam Tuesday, April 13, 7:00 PM Room TBD Same procedure You must e-mail your instructor with: Class number of conflict Instructors name Time of class

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)

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 ) 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 } Initialize variables Link pointer to file Read data (fscanf) Print 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

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 ) 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 Initialize variables Open file Input choice Scan files Print

Scan files Print 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", Scan files Print

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 } Close file

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: 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

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 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

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

/* 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++;

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';

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

// 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);

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); }

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