PL1 Lecture 09
PL1 Lecture 09
Programming Languages 1
Lecture 9
File Processing [ Part 2 ], Sequential / Random
Access, Bitwise Operations, .. & .. Command-Line
Arguments
Course & Lectures are based on their counterparts in the following courses:
o Harvard University's CS50; An introduction to the intellectual enterprises of
computer science and the art of programming, Harvard School of
Engineering and Applied Sciences.
o UNSW's CS1: Higher Computing, by Richard Buckland – The University of
New South Wales.
o MIT's 6.087 Practical Programming in C (2010), 6.096 Introduction to C++
(2011), and 6.S096 Introduction to C and C++ (2013), MIT (Massachusetts
1
Institute of Technology) OpenCourseWare.
2
Outline l
i • Files: Random Access
n
• Bitwise
e Operations
• Using Command-Line Arguments
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 Recap ..
Why Files?
12
Sequential Access Files .. TEXT FILES
• Use fprintf and fscanf, both of which are similar to their friends printf and
scanf, except that you must pass the FILE pointer as the first argument.
E.g., fscanf( fPtr, "%d %s", &account, name);
• fscanf and fprintf can deal with the standard input/output by using stdin
or stdout as the file pointer, as in:
fprintf( stdout, "%s \n", “test”);
• When you have finished working with a file, you should close it using the
function:
int fclose(FILE *a_file);
fclose returns a zero if the file is closed successfully. Closing a file can free
resources for which other users or programs may be waiting for.
Example
Sequential When
If theaprogram
you use reaches
string literal, the end of the
use double
Access Files file, Function
backslashes feof than
rather returns
Reading
Open
true backslash!
a single
file
from
for
after the It's
just the way
program quoted
attempts strings
to read are
the
File Pointer handled in C
nonexistent
int account; and reading
file
dataC++.
following the last line.
char name[ 30 ];
double balance;
FILE *fPtr;
if ( ( fPtr = fopen( "c:\\clients.dat", "r" ) ) == NULL )
printf( "File could not be opened\n" );
else {
Means the file can’t
printf( "%-10s%-13s%s\n", "Account", be opened
"Name", "Balance" );
fscanf( fPtr, "%d%s%f", &account, name, &balance );
while (!feof( fPtr ) ) {
printf( "%-10d%-13s%7.2f\n", account, name, balance );
fscanf( fPtr, "%d%s%f", &account, name, &balance );
} /* end while */
fclose( fPtr );
}
Sequential Access Files .. TEXT FILES
Resetting the File Position Pointer
• A program normally starts reading from the beginning of the file,
and reads all data consecutively (sequentially, one after the other).
• The File position pointer is an integer that indicates the index of
the next byte in the file to be read or written. This is sometimes
referred to as the file offset.
• The file position pointer is a member of the FILE structure
associated with each file.
• A statement rewind( fPtr ) causes a program’s file position pointer
to be repositioned to the beginning of the file (i.e., byte 0).
Random Access Files .. BINARY
FILES
• Random access means we can move to any part of a file and
read or write data from it without having to read through the
entire file.
• Individual records of a random access file are normally fixed in
length and may be accessed directly (and thus quickly).
• The exact location of a record relative to the beginning of the
file can be calculated.
• Data can be inserted in a random-access file without
destroying other data in the file. Data stored previously can
also be updated or deleted without rewriting the entire file.
Random Access Files .. BINARY
FILES
• Function fread reads a specified number of bytes from the
location in the file specified by the file position pointer
• Function fwrite writes a specified number of bytes to a file.
The data is written beginning at the location in the file
indicated by the file position pointer.
• Example:
fwrite( &data, sizeof( int ), 1, fPtr );
In sequential access it was:
fprintf( fPtr, "%d", number );
Random Access Files .. BINARY
FILES
Function fseek sets the file position pointer to a specific position
in the file, then fwrite writes the data.
int fseek( FILE *stream, long int offset, int whence );
offset: is the number of bytes to seek from location whence in
the file pointed to by stream.
22
Bitwise Operators
27
Command-Line Arguments
It’s possible to pass arguments to the main from a command line by including
parameters: int argc and char *argv[] in the parameter list of the main.
argc: argument count (an integer variable)
The number of arguments that are passed to main in the argument vector
argv.
The value of argc is always one greater than the number of command-line
arguments that the user enters.
argv: argument vector (array of strings)
An array of string pointers passed to the C program's main function.
argv[ 0 ] is always the name of the command (program name).
argv[ argc ] is a null pointer.
Command-Line Arguments .. An Example
Calculate x to the power of y
int main( int argc, char *argv[ ] ) {
if ( argc != 3 ) { printf( "ERROR!!\n" ); // file name and 2 parameters
} else {
int x, y, power = 1, i;
x = argv[ 1 ][ 0 ] - '0'; // to convert the string to an integer
y = argv[ 2 ][ 0 ] - '0';
for( i = 1; i <= y; i++ )
power*= x;
printf( "\t%d", power );
}
Thanks! .. Questions?
30