Beruflich Dokumente
Kultur Dokumente
EOF
File Buffer Data Flow
DISK MEMORY
File Buffer
File Pointer
a Appending(create file) ab
d = 0;
Date input;
long int inputArray[5];
fp = fopen("myFile", "rb");
fread(&d, sizeof(double), 1, fp);
fread(inputArray, sizeof(int), 5, fp);
fread(&input, sizeof(Date), 1, fp);
printf("%lf", d); //Output 27.5
Moving File Position
When you open a file you read from the beginning to the
end.
It is possible, however, to move the file pointer as
needed.
fseek – move to a specified location, relative to the
beginning, end, or current position in the file
fgetpos – stores the current file position
fsetpos – returns to a previously stored file position
ftell – returns the current file position as a long int
rewind – put the file position at 0 (zero)
Learning File Types
You may read standards on how a file is stored.
.rgb may be binary, but is a simple RGB format for every
pixel.
.csv is a text file with commas separating values in the row
of a table.
Very useful for handling large amounts of data for analysis.
Excel can read in and output .csv files.
You may also create your own file type.
Follows your rules for outputting binary or separating out
text.
Concepts Summary
Files can be thought of as a sequence of bytes on an external
storage medium
Files can be text or binary
Reading text files can be complicated. Read the descriptions of the
format specifiers carefully. Don't guess! Incorrect format specifiers
will do something, and might appear to work, but the behavior is
really indeterminate.
It is impossible to read a binary file without knowing what the bytes
represent – the data types and their locations in the file.
Binary files can be read and written with large blocks of data or a
byte at a time using fread and fwrite
You can mix binary and text file access, but it shouldn't be done.
The behavior is indeterminate, and tends to result in guessing what
the code will do, and then changing it – over and over.
Reading/Writing Text File
Open a file for text writing. Put all of the text in the file
that appears on this slide. Close the file.
Open the file for reading. Read the file one character at a
time, counting how many of each vowel occur in the text.
Close the file, and then open it for reading, this time
counting how many words are in the file.
Generate 1000 random integers between 1 and 100 inclusive, writing them
into the file as they are generated.
Read in all the values, and calculate their sum as they are read. (no arrays
needed) Find and output the average of the numbers.
Repeat the exercise above using an array. Read all the values from the file
in one statement.
Just for fun... If you seed the random number generator with the value
123, use fseek to read the 30th number in the file. What is it?
Cipher Example
A cipher is a way of writing a coded message
The simplest form is a substitution cipher
Substitute one character for another:
Every a is changed to z
Every b is changed to y
Etc…
Our goal: Read a file that explains the cipher
Create a struct that holds the original character and the cipher
character
Read the cipher file line by line filling out the struct
Cipher array is of size 52
Concepts Summary
Don't mix the use of functions intended for Text and
Binary files.
Keep a reference for the description of the format
specifiers handy!
Close the files as soon as you are done with them.
Draw pictures of a binary file – use graph paper to help
keep track of the bytes.