fread
Lecture Overview
Focus on file Input/Output (I/O) in software development with examples.
Definitions and Characteristics of Files
A file provides persistent storage of data.
Data can be in text or binary format:
Text files: Lines are terminated with an end-of-line marker, e.g., ‘\n’ on UNIX systems.
Binary files: Stored in the format that the computer understands without a specific end-of-line marker.
Files can be accessed in two ways:
Serial access: Reading data sequentially.
Random access: Jumping or ‘seeking’ to specific data within the file.
File operations require opening a file prior to access and closing it afterwards.
File Access Methods
Serial Access
Read the file sequentially from start to end.
Conceptual mechanism: A ‘file pointer’ moves through the file as data is read.
Example of file-reading procedure:
Open the file.
Read line 1.
Read line 2.
…
Continue until the end of the file.
File Opening and Closing
Declare a pointer to a file structure:
FILE *fp;
Open a text file for reading:
fp = fopen("input.txt", "r");
Note: The file pointer may be
NULL, indicating an invalid pointer if the file does not exist or can't be opened.Closing the file:
fclose(fp);
File Copying Challenge
Goal: Copy a file if it exists:
Ensure the source file exists.
Ensure the destination file is valid for writing.
Copy data from the input file to output until reaching the end of the input file.
File Copying Example
/* filecopy: copy file ifp to file ofp */
void filecopy(FILE *ifp, FILE *ofp) {
int c;
while ((c = getc(ifp)) != EOF)
putc(c, ofp);
}
int main() {
FILE *inFile = fopen("input.txt", "r"),
*outFile = fopen("output.txt", "w");
if (inFile && outFile) {
filecopy(inFile, outFile);
fclose(outFile);
fclose(inFile);
}
return 0;
}
Equivalent validation:
if (inFile != NULL && outFile != NULL)
Working with Lines of Text in Text Files
Use the function
fgetsto read a line from a file:
char *fgets(char *buf, int maxline, FILE *fp);
Typical execution:
while (fgets(line, 80, fp) != NULL) {
// Process line
}
Note: If the line exceeds 80 characters, handling is necessary to prevent overflow problems.
Additional input/output functions include
fprintf,fscanf, andfputs.
Pattern Finding Challenge in Text Files
Aim: Identify a pattern within a text file.
Read the file line by line using
stdio.h.Search each line for the specified pattern utilizing methods from
strings.h.Keep a count of occurrences of the pattern.
Important considerations must be made regarding line length and character encoding.
Example of a Log File
Sample format of line entries within a text file:
46.4.84.242 - - [31/Jul/2011:16:40:46 +0100] "GET /2009/10/create-tv-episode-1/ HTTP/1.1" 200 1684578 "-" "Python-urllib/2.7"
Data includes IP addresses, timestamps, requests, response codes, and user agent information.
Binary File Handling
Record Access Requirements
Objective: Read and write binary data without text markers.
Precondition: Validation against file specifications to ensure correct binary format.
Reading and Writing Structures in Binary Files
To operate with binary files:
Use
freadfor reading blocks of data:size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);ptr: Pointer to the block of memory where data is read into.size: Size in bytes of each element to be read.nmemb: Number of elements, each one with a size ofsizebytes.stream: Pointer to aFILEobject that specifies an input stream.
Use
fwritefor writing blocks of data:size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);ptr: Pointer to the block of memory containing the data to be written.size: Size in bytes of each element to be written.nmemb: Number of elements, each one with a size ofsizebytes.stream: Pointer to aFILEobject that specifies an output stream.
File mode for binary files should be set with 'b', e.g.,
"rb"for read binary or"wb"for write binary.fseekandftellfunctions are used for random access within binary files to move the file pointer to specific offsets.