Download - Week13.pptx
-
7/27/2019 Week13.pptx
1/38
CS1010: Programming Methodologyhttp://www.comp.nus.edu.sg/~cs1010/
http://www.comp.nus.edu.sg/~cs1010/http://www.comp.nus.edu.sg/~cs1010/ -
7/27/2019 Week13.pptx
2/38
Week 13: File Processing
Objectives:
Understand concepts of file I/O
Learn how to use stdlib functions for formatted,
character and line I/O
References:
Chapter 3, Chapter 7
Lessons 3.6, 3.8, 7.4
Week13 - 2CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
3/38
Week 13: Outline
1. Introduction
2. Opening a File and File Modes
3. Closing a File
4. I/O Functions
5. Formatted I/O with Demo #1
6. Detecting End of File & Errors with Demo #2
7. Character I/O with Demo #38. Exercise #1: Trimming Blanks
9. Line I/O with Demo #4
10. Video on Data Display Debugger (DDD)
Week13 - 3CS1010 (AY2011/2 Semester 1)
0. Week 12 Exercise #3: Health Screen
-
7/27/2019 Week13.pptx
4/38
Wk12. Ex #3 (take-home): Health Screen (1/2)
CS1010 (AY2011/2 Semester 1) Week13 - 4
Write a program Week12_Health_Screen.c to read in alist of health screen readings
Each input line represents a reading consisting of 2 numbers:
a float value indicating the health score, and an int value
indicating the number of people with that score
You may assume that there are at most 100 readings
The input should end with the reading 0 0, or when 100
readings have been read.
As the readings are gathered from various clinics,
there might be duplicate scores in the input. You are todetermine how many unique scores there are.
A skeleton program Week12_Health_Screen.c is given.
Well discuss this at our next lecture.
-
7/27/2019 Week13.pptx
5/38
Wk12. Ex #3 (take-home): Health Screen (2/2)
CS1010 (AY2011/2 Semester 1) Week13 - 5
A sample run is shown belowEnter score and frequency (end with 0 0):5.2135 33.123 42.9 30.87 2
2.9 28.123 63.123 27.6 32.9 40.111 5
0 0Number of unique readings = 7
Possible extension: Which is the score that has the
highest combined frequency? (Do this on your own.)
-
7/27/2019 Week13.pptx
6/38
1. Introduction: Input/Output
In C, input/output is done based on the concept of astream
A stream can be a file or a consumer/producer of data
Examples: screen, keyboard, hard disk, printer, network port
A stream is accessed using file pointervariable oftype FILE *
The I/O functions/macros are defined in header file
stdio.h Two types of streams: text and binary
Week13 - 6CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
7/38
1. Introduction: Text vs Binary Streams
Text stream Consists of a sequence of characters organized into lines
Each line contains 0 or more characters followed by a
newline character \n
Text streams stored in files can be viewed/edited easily Example: source code of C program
Binary stream
Beyond the scope of CS1010
Week13 - 7CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
8/38
1. Introduction: Standard Streams
Three streams are predefined for each C program:stdin, stdout, stderr
stdin points to a default input stream (keyboard)
stdout points to a default output stream (screen)
stderrpoints to a default output stream for error messages(screen)
printf writes output to stdout
scanf reads input from stdin
The 3 standard streams do not need to be declared,
opened, and closed
Week13 - 8CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
9/38
1. Introduction: Constants
There are two useful constants used in file processing NULL: null pointer constant
EOF: used to represent end of file or error condition
Week13 - 9CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
10/38
2. Opening a File and File Modes (1/2)
Prototype:
FILE *fopen(const char *filename, const char *mode)
ReturnsNULL if error; otherwise, returns a pointer ofFILE type
Possible errors: non-existent file, or dont have
permission to open the file
File mode depends on intended file operations
Week13 - 10CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
11/38
2. Opening a File and File Modes (2/2)
File modes for text files:
Week13 - 11CS1010 (AY2011/2 Semester 1)
Mode Meaning
r Open for reading
w Open for writing (file needs not exist)
a Open for appending (file needs not exist)
r+ Open for reading and writing, starting at beginning
w+ Open for reading and writing (truncate if file exists)
a+ Open for reading and writing (append if file exists)
We will focus only on r and w
Note that In r mode, file must already exist
In w mode, new data overwrite existing data (if any)
-
7/27/2019 Week13.pptx
12/38
3. Closing a File
Prototype:
int fclose(FILE *fp)
Allows a file that is no longer used to be closed
Returns EOF if error is detected; otherwise, returns 0
It is good practice to close a file after use.
Week13 - 12CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
13/38
4. I/O Functions
Formatted I/O: fprintf, fscanf Uses format strings to control conversion between character
and numeric data
Character I/O: fputc,putc ,putchar , fgetc ,
getc , getchar , ungetc Reads and writes single characters
Line I/O: fputs,puts , fgets , gets
Reads and writes lines
Used mostly for text streams
Block I/O: fread, fwrite
Used mostly for binary streamsWeek13 - 13CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
14/38
5. Formatted I/O (1/4)
Uses format strings to control conversion betweencharacter and numeric data fprintf: converts numeric data to character form and
writes to an output stream
fscanf: reads and converts character data from an input
stream to numeric form
Both fprintf and fscanf functions can have
variable numbers of arguments
Example:
Week13 - 14CS1010 (AY2011/2 Semester 1)
float weight, height;FILE *fp1, *fp2;. . .fscanf(fp1, "%f %f", &weight, &height);fprintf(fp2, "Wt: %f, Ht: %f\n", weight, height);
-
7/27/2019 Week13.pptx
15/38
5. Formatted I/O (2/4)
fprintf returns a negative value if an error occurs;otherwise, returns the number of characters written
fscanf returns EOF if an input failure occurs before
any data items can be read; otherwise, returns the
number of data items that were read and stored
Week13 - 15CS1010 (AY2011/2 Semester 1)
=printf(" "); fprintf(stdout, " ");
scanf(" "); fscanf(stdin, " ");=
-
7/27/2019 Week13.pptx
16/38
5. Formatted I/O (3/4): Demo #1
Week13 - 16CS1010 (AY2011/2 Semester 1)
#include int main(void){
FILE *infile, *outfile;char x;int y;
float z;
infile = fopen("demo1.in", "r");outfile = fopen("demo1.out", "w");
fscanf(infile, "%c %d %f", &x, &y, &z);
fprintf(outfile, "Data read: %c %d %.2f\n", x, y, z);
fclose(infile);fclose(outfile);return 0;
}
Week13_Demo1.c
File demo1.in:10 20 30
Whats the output in demo1.out?
-
7/27/2019 Week13.pptx
17/38
5. Formatted I/O (4/4): Demo #1 ver2
Week13 - 17CS1010 (AY2011/2 Semester 1)
#include #include int main(void){
. . .
if ((infile = fopen("demo1.in", "r")) == NULL){printf("Cannot open file demo1.in\n");exit(1);
}if ((outfile = fopen("demo1.out", "w")) == NULL)
{printf("Cannot open file demo1.out\n");exit(2);
}. . .
}
Week13_Demo1_v2.c
Check if file can
be opened.
To use exit()
-
7/27/2019 Week13.pptx
18/38
6. Detecting End of File & Errors (1/2)
Each stream is associated with two indicators: errorindicator& end-of-file (EOF) indicator
Both indicators are cleared when the stream is opened
Encountering end-of-file sets end-of-file indicator
Encountering read/write error sets error indicator An indicator once set remains set until it is explicitly cleared
by calling clearerr or some other library function
feof returns a non-zero value if the end-of-file
indicator is set; otherwise returns 0 ferror returns a non-zero value if the error indicator
is set; otherwise returns 0
Need to include Week13 - 18CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
19/38
6. Demo #2: Using feof( ) (2/2)
Caution on using feof()
Week13 - 19CS1010 (AY2011/2 Semester 1)
#include #include
int main(void)
{. . .
while (!feof(infile)){
fscanf(infile, "%d", &num);printf("Value read: %d\n", num);
}. . .
}
Week13_Demo2.c
Value read: 10Value read: 20Value read: 30
Value read: 30
Output:
10 20 30
Input file demo2.in
Why does the last line appear twice?
To be discussed in Week 13 discussion.
-
7/27/2019 Week13.pptx
20/38
7. Character I/O: Output Functions (1/4)
Output functions: fputc,putchar
Week13 - 20CS1010 (AY2011/2 Semester 1)
fputc andputchar return EOF if a write erroroccurs; otherwise, return character written
int ch = 'A';FILE *fp;
putchar(ch); // writes ch to stdout
fp = fopen( ... );fputc(ch, fp); // writes ch to fp
-
7/27/2019 Week13.pptx
21/38
7. Character I/O: Input Functions (2/4)
Input functions: fgetc, getchar , ungetc
Week13 - 21CS1010 (AY2011/2 Semester 1)
fgetc and getchar return EOF if a read error occurs
or end of file is reached; otherwise, return character
read Need to call eitherfeof orferror to distinguish the 2 cases
int ch;FILE *fp;ch = getchar(); // reads a char from stdinfp = fopen( ... );
ch = fgetc(fp); // reads a char from fp
-
7/27/2019 Week13.pptx
22/38
7. Character I/O: Demo #3 (3/4)
Week13 - 22CS1010 (AY2011/2 Semester 1)
int copyFile(char *sourcefile, char *destfile){FILE *sfp, *dfp;if ((sfp = fopen(sourcefile, "r")) == NULL)
exit(1); // error - can't open source fileif ((dfp = fopen(destfile, "w")) == NULL) {
fclose(sfp); // close source fileexit(2); // error - can't open destination file
}while ((ch = fgetc(sfp)) != EOF) {
if (fputc(ch, dfp) == EOF) {fclose(sfp); fclose(dfp);
exit(3); // error - can't write to file}
}fclose(sfp); fclose(dfp);return 0;
}
Week13_CopyFile.c
-
7/27/2019 Week13.pptx
23/38
7. Character I/O: ungetc (4/4)
ungetc pushes back a character read from a stream
ungetc returns the character it pushes back
Example: Read a sequence of digits and stop at the
first non-digit
Week13 - 23CS1010 (AY2011/2 Semester 1)
int ch;FILE *fp = fopen( ... );
while (isdigit(ch = getc(fp))){
// process digit read. . .
}ungetc(ch, fp); // pushes back last char read
-
7/27/2019 Week13.pptx
24/38
8. Exercise #1: Trimming Blanks
Write a program Week13_TrimBlanks.c that containsa function
int trimBlanks(char *infile, char *outfile)
that takes an input text file and produces a new text
file that is a duplicate copy of the input file except thateach sequence of consecutive blank characters is
replaced by a single blank character.
The function returns -1 if there is an error; otherwise,
it returns the number of blank characters trimmed. An incomplete program Week13_TrimBlanks.c is
given. A test input file trimblanks.in is also given.
Week13 - 24CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
25/38
9. Line I/O: Output Functions (1/6)
Output functions: fputs,puts
Week13 - 25CS1010 (AY2011/2 Semester 1)
fputs andputs return EOF if a write error occurs;otherwise, return a non-negative number
FILE *fp;
// writes to stdout with newline character appendedputs("Hello world!");
fp = fopen( ... );// writes to fp without newline character appendedfputs("Hello world!", fp);
-
7/27/2019 Week13.pptx
26/38
9. Line I/O: Input Functions (2/6)
Input functions: fgets, gets
Week13 - 26CS1010 (AY2011/2 Semester 1)
fgets and gets store a null character at the end of the string
fgets and gets return a null pointer if a read error occurs orend-of-file is encountered before storing any character;otherwise, return first argument
Avoid using gets due to security issue
char s[100];FILE *fp;
gets(s); // reads a line from stdin
fp = fopen( ... );fgets(s, 100, fp); // reads a line from fp
-
7/27/2019 Week13.pptx
27/38
9. Line I/O: fgets (3/6)
Prototype:char *fgets(char *s, int n, FILE *fp) s is a pointer to the beginning of a character array
n is a count
fp is an input stream
Characters are read from the input stream fp into s
until a newline character is seen,
end-of-file is reached, or
n 1 characters have been read without encounteringnewline character or end-of-file
If the input was terminated because of a newline
character, the newline character will be stored in thearray before the terminating null character (\0)
Week13 - 27CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
28/38
9. Line I/O: fgets (cont.) (4/6)
If end-of-file is encountered before any charactershave been read from the stream, fgets returns a null pointer
The contents of the array s are unchanged
If a read error is encountered, fgets returns a null pointer The contents of the array s are indeterminate
WheneverNULL is returned, feoforferror shouldbe used to determine the status
Week13 - 28CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
29/38
9. Demo #5: Counting Lines (5/6)
Write a function that takes as input the name of a textfile and returns the number of lines in the input file.
If an error occurs, the function should return a
negative number.
Assume that the length of each line in the file is at
most 80 characters.
Week13 - 29CS1010 (AY2011/2 Semester 1)
-
7/27/2019 Week13.pptx
30/38
9. Demo #5: Counting Lines (6/6)
Week13 - 30CS1010 (AY2011/2 Semester 1)
#define MAX_LINE_LENGTH 80int countLines(char *filename){
int count = 0;FILE *fp;char s[MAX_LINE_LENGTH+1];
if ((fp = fopen(filename, "r")) == NULL)return -1; // error
while (fgets(s, MAX_LINE_LENGTH+1, fp) != NULL)count++;
if (!feof(fp)) // read error encounteredcount = -1;
fclose(fp);return count;
}
Week13_CountLines.c
-
7/27/2019 Week13.pptx
31/38
10. Data Display Debugger (DDD)
See video
http://www.youtube.com/watch?v=VF7IBEAA8Ig
Week13 - 31CS1010 (AY2011/2 Semester 1)
http://www.youtube.com/watch?v=VF7IBEAA8Ighttp://www.youtube.com/watch?v=VF7IBEAA8Ig -
7/27/2019 Week13.pptx
32/38
Summary for Today
CS1010 (AY2011/2 Semester 1) Week13 - 32
In todays lecture we learnt
I/O on text files
I/O functions:
File operations: fopen, fclose, feof, ferror Formatted I/O: fprintf, fscanf
Character I/O: fputc, putchar, fgets, getchar, ungetc
Line I/O: fputs, puts, fgets, gets
-
7/27/2019 Week13.pptx
33/38
CS1010 (AY2011/2 Semester 1) Week13 - 33
Post-CS1010 (1/2)
For CS/CM/CB students: CS1010 CS1020 CS2010orCS1010 CS2020 (accelerated)
For IS and EC students: CS1010 CS1020
CS1020 Data Structures and Algorithms I Emphasis on algorithms and data structures
Using an object-oriented programming language Java
Textbook: Data Abstraction & Problem Solving with Java: Walls
and Mirrors by F. Carrano and J. Prichard
-
7/27/2019 Week13.pptx
34/38
CS1010 (AY2011/2 Semester 1) Week13 - 34
Post-CS1010 (2/2)
CS1020 has sit-in labs (like mini-PEs) every otherweek
Strengthen your programming skills to prepare for
CS1020
-
7/27/2019 Week13.pptx
35/38
CS1010 (AY2011/2 Semester 1) Week13 - 35
CS1010 Final Examination
CS1010 Exam 21 November 2011, Monday, 1 3pm
Venue: To be announced by Registrars Office
Format MCQ section: 6 questions
5 other questions (comprising tracing questions and
programming questions)
Grading of CS1010 is not based on bell curve
-
7/27/2019 Week13.pptx
36/38
CS1010 (AY2011/2 Semester 1) Week13 - 36
How to Prepare for Exams?
Preparing for Exams: A Guide for NUS Students http://www.cdtl.nus.edu.sg/examprep/
http://www.cdtl.nus.edu.sg/examprep/http://www.cdtl.nus.edu.sg/examprep/ -
7/27/2019 Week13.pptx
37/38
CS1010 (AY2011/2 Semester 1) Week13 - 37
-
7/27/2019 Week13.pptx
38/38
End of File