Example file fp int c if fpfopendatatxt rnull

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 500 { Lecture 99 9 Inquiring End-Of-File | feof() Function Prototype: #include <stdio.h> int feof(FILE *fp) Function Description / Return Values: Returns non-zero (TRUE) when EOF has previously been detected for the stream fp, otherwise zero (FALSE). Example: FILE *fp int c if ((fp=fopen("data.txt", "r"))==NULL) { printf("Cannot open file.\n") exit(0) } ... if (!feof(fp)) c = fgetc(fp) ... CSC 1500 { Lecture 99 10 Example | Data File Processing #include <stdio.h> #include <string.h> int main(void) { FILE *fin int code char name 30] float price, quantity if ((fin=fopen("client.dat", "r"))==NULL) printf("Error: Cannot open %s.\n", "clients.dat") else { printf("%-7s%-20s%15s%10s%10s\n", "Code", "Name", "Unit Price", "Quantity", "Amount") fscanf(fin, "%d%s%f%f", &code, name, &price, &quantity) while (!feof(fin)) { printf("%-7d%-20s%15.2f%10.2f%10.2f\n", code, name, price, quantity, price*quantity) fscanf(fin, "%d%s%f%f", &code, name, &price, &quantity) } fclose(fin) } return 0 } 100 200 300 400 500 Erase Box-File Correction-Fluid Time-Magazine A4-Paper 4.9 25.00 13.50 34.00 28.50 10 5 15 2 5 client.dat Code 100 200 300 400 500 Name Erase Box-File Correction-Fluid Time-Magazine A4-Paper Unit Price 4.90 25.00 13.50 34.00 28.50 Quantity 10.00 5.00 15.00 2.00 5.00 Amount 49.00 125.00 202.50 68.00 142.50 CSC 1500 { Lecture 99 11 Readings Chapter 13, Sections 13.1 { 13.5 Chapter 13, Section 13.8 (only if you are interested) Chapter 13, Sections 13.9 { 13.10 Exercises Chapter 13, Exercises 7, 8, 12, 15, 18, 19 2 End of Lecture 99 CSC 1500 { Lecture 99 12...
View Full Document

Ask a homework question - tutors are online