CS112 Programming Languages 1 (Lecture 8 - Spring 2020)
CS112 Programming Languages 1 (Lecture 8 - Spring 2020)
Programming Languages 1
( General Division – Software Engineering Program – Medical Informatics Program )
Lecture 8
File Processing [ Input / Output ]
• C File Processing
• Why Files?
• Files and Streams.
O • Standard Input, Standard Output.
u • Create a Reference to the File (a File
t Pointer).
Outline l • Open the File (using the fopen function).
i
n • Read from the File.
e • Write to the File.
• The Data Hierarchy.
2
The ability to read data from and write data to files is the primary means of
storing persistent (permanent) data, or data which does not disappear when
your program finishes running.
File I/O
We are used to reading from and writing to the terminal:
• read from stdin (Keyboard)
• write to stdout (Monitor)
• write to stderr (Monitor)
FILE* fptr;
FILE* fptr;
Some Operating Systems, such as UNIX, will close any files you’ve left open when
your program terminates, but it is considered poor coding practice to rely on this
behavior.
fclose(fptr);
Remember!
✓ Always open a file before reading from or writing to it.
✓ Always check the return value to make sure you don't get back a NULL.
✓ Always close a file if you open it.
The Data Hierarchy
o Bit – smallest data item
o Value of 0 or 1
o Byte – 8 bits
o Used to store a character
o Decimal digits, letters, and special symbols
o Field – group of characters conveying meaning
o Example: your name
o Record – group of related fields
o Represented by a struct or a class
o Example: In a payroll system, a record for a particular
employee that contained his/her identification number,
name, address, etc.
o File – group of related records
o Example: payroll file
o Database – group of related files
The Data Hierarchy
Example #1
Writing to a file
#include <stdio.h>
#define STUDENTS 3
int main(void)
{
int scores[] = { 96, 90, 83 };
FILE* file = fopen(“database”, “w”);
if (file != NULL)
{
for (int i = 0; i < STUDENTS; i++)
{
fprintf(file, “%i\n”, scores[i]);
}
fclose(file);
}
}
In this program, we are
storing the values of an
array called scores in a Example #1
file named database.
Writing to a file
#include <stdio.h> We open the file using the fopen function. The
#define STUDENTS 3 second argument "w" tells fopen that we want to
int main(void) write to this file. If a file named database does not
{ already exist, fopen will create it.
int scores[] = { 96, 90, 83 };
FILE* file = fopen(“database”, “w”);
if (file != NULL) if (file != NULL) is
{ a sanity check that the call
for (int i = 0; i < STUDENTS; i++) to fopen didn’t fail.
{
fprintf(file, “%i\n”, scores[i]);
}
fclose(file);
}
fprintf takes a file pointer as its first argument. Instead of printing to
}
the screen, fprintf prints to the file we pass to it.
1 /* Fig. 11.3: fig11_03.c
2 Create a sequential file */
3 #include <stdio.h>
4
5
6
int main()
{ Example #2
7
8
int account; /* account number */
char name[ 30 ]; /* account name */
Writing to a file
9 double balance; /* account balance */
10
11 FILE *cfPtr; /* cfPtr = clients.dat file pointer */
12
13 /* fopen opens file. Exit program if unable to create file */
14 if ( ( cfPtr = fopen( "clients.dat", "w" ) ) == NULL ) {
15 printf( "File could not be opened\n" );
16 } /* end if */
17 else {
18 printf( "Enter the account, name, and balance.\n" );
19 printf( "Enter EOF to end input.\n" );
20 printf( "? " );
21 scanf( "%d%s%lf", &account, name, &balance );
22
23 /* write account, name and balance into file with fprintf */
24 while ( !feof( stdin ) ) {
25 fprintf( cfPtr, "%d %s %.2f\n", account, name, balance );
26 printf( "? " );
27 scanf( "%d%s%lf", &account, name, &balance );
28 } /* end while */ Example #2
29
30 fclose( cfPtr ); /* fclose closes file */ Writing to a file
31 } /* end else */
32
33 return 0; /* indicates successful termination */
34
35 } /* end main */
Program Output:
Enter the account, name, and balance.
Enter EOF to end input.
? 100 Jones 24.98
? 200 Doe 345.67
? 300 White 0.00
? 400 Stone -42.16
? 500 Rich 224.62
? ^Z
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; /* account number */ Example #3
8 char name[ 30 ]; /* account name */
9
10
double balance; /* account balance */ Reading from a file
11 FILE *cfPtr; /* cfPtr = clients.dat file pointer */
12
13 /* fopen opens file; exits program if file cannot be opened */
14 if ( ( cfPtr = fopen( "clients.dat", "r" ) ) == NULL ) {
15 printf( "File could not be opened\n" );
16 } /* end if */
17 else { /* read account, name and balance from file */
18 printf( "%-10s%-13s%s\n", "Account", "Name", "Balance" );
19 fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
20
21 /* while not end of file */
22 while ( !feof( cfPtr ) ) {
23 printf( "%-10d%-13s%7.2f\n", account, name, balance );
24 fscanf( cfPtr, "%d%s%lf", &account, name, &balance );
25 } /* end while */
26
27 fclose( cfPtr ); /* fclose closes the file */
28 } /* end else */
29
30 return 0; /* indicates successful termination */
31
32 } /* end main */
Example #3
Reading from a 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
Thanks! .. Questions?
20