File handling is a fundamental skill for any C programmer, enabling programs to interact with persistent data on a computer’s disk for tasks like managing records, storing configurations, and logging activity.
This comprehensive guide presents 25+ C Programming file handling exercises designed to take you from basics to advanced techniques.
Each coding exercise includes a Practice Problem, Hint, Solution code, and detailed Explanation, ensuring you don’t just copy code, but genuinely understand how and why it works.
Also, See: C Programming Exercises with 9 topic-wise sets and 275+ practice questions.
What You’ll Practice
The challenges cover the following core topics:
- Basic Text I/O: Reading, writing, and appending text files using fundamental functions like
fopen(),fclose(),fprintf(), andfscanf(). - Character Processing: Character-by-character I/O with
fgetc()andfputc()for tasks like word counting and analysis. - Access Methods: Using
fseek()andftell()for Sequential and Random Access to navigate files and calculate size. - Binary Data: Storing and retrieving complex data structures (structs) in binary format using
fwrite()andfread(). - Error Management: Implementing robust checks for file failures and handling system-level errors with
perror().
+ Table of Contents (26 Exercises)
Table of contents
- Exercise 1: Create and Write to a File
- Exercise 2: Read and Display File Content
- Exercise 3: Append Data to a File
- Exercise 4: Count Characters in a File
- Exercise 5: Count Lines in a File
- Exercise 6: Count Words in a File
- Exercise 7: Copy File
- Exercise 8: Display a Specific Line of a File
- Exercise 9: Write a Series of Numbers to a File
- Exercise 10: Read Numbers and Calculate Sum
- Exercise 11: Uppercase Conversion
- Exercise 12: Remove Vowels from a File
- Exercise 13: Search for a Word
- Exercise 14: Replace Word in a File
- Exercise 15: Display File Content in Reverse
- Exercise 16: Merge Two Files
- Exercise 17: Tab to Spaces Conversion
- Exercise 18: Simple Encryption (Caesar Cipher)
- Exercise 19: Simple Decryption (Caesar Cipher)
- Exercise 20: Write Student Record (Binary)
- Exercise 21: Read Student Record (Binary)
- Exercise 22: Multiple Records (Binary)
- Exercise 23: Search and Display Record (Binary)
- Exercise 24: Update Record (Binary)
- Exercise 25: Determine File Size
- Exercise 26: Robust Error Checking
Exercise 1: Create and Write to a File
Practice Problem: Develop a C program to create a new text file named data.txt and write two lines of personal information (e.g., your name and age) to it using fprintf().
Expected Output:
Data successfully written to data.txt.
data.txt
Name: Alice Johnson
Age: 30 years
+ Hint
Use fopen() with the write mode ("w") to create or overwrite the file. Remember to check if the file pointer is NULL to handle potential errors, and use fclose() when finished.
+ Show Solution
Explanation:
- The program first declares a
FILEpointer,fp. It attempts to opendata.txtin write mode ("w"). If the file doesn’t exist, it’s created; if it exists, its content is truncated (deleted). The program checks iffpisNULL, which indicates a failure to open the file. - If successful,
fprintf()is used exactly likeprintf(), but its first argument is the file pointer, directing the output to the file instead of the console. Finally,fclose(fp)is called to close the file, flushing any remaining buffers and releasing the file resource.
Exercise 2: Read and Display File Content
Practice Problem: Read the entire content of the existing file data.txt (created in Exercise 1) and display it line by line on the console using fscanf() or fgets().
Expected Output:
Name: Alice Johnson
Age: 30 years
+ Hint
Open the file in read mode ("r"). Use a loop with fgets() to read strings line by line until it returns NULL (indicating the End-Of-File, or EOF, is reached).
+ Show Solution
Explanation:
- The file is opened in read mode (
"r"). A character array,buffer, is used as a temporary storage space for each line read. - The
fgets()function reads a line from the file stream (fp) and stores it intobuffer, reading at mostsizeof(buffer) - 1characters, or until a newline character or EOF is encountered. - The loop continues as long as
fgets()returns a non-NULL value. The content of the buffer is then printed to the standard output usingprintf().
Exercise 3: Append Data to a File
Practice Problem: Open the existing file data.txt(created in Exercise 1) in append mode and add today’s date and a short message (“Successfully appended.”) as new lines to the end of the file.
Expected Output:
data.txt
Name: Alice Johnson
Age: 30 years
Date Appended: 2025-10-15
Status: Successfully appended.
+ Hint
Use fopen() with the append mode ("a"). The append mode ensures that any new data is written to the end of the existing file content without overwriting it.
+ Show Solution
Explanation:
The core difference here is the use of the append mode ("a") in fopen(). When a file is opened in this mode, the file pointer is automatically positioned at the end of the file. Subsequent write operations using fprintf() or other output functions will simply add the new data after the existing content, preserving the original data.
Exercise 4: Count Characters in a File
Practice Problem: Write a program to read a text file (e.g., data.txt updated in Exercise 3) character by character and count the total number of characters in it, including spaces and newlines.
Expected Output:
Total number of characters in the file: 91
+ Hint
Open the file in read mode ("r"). Use a loop with the fgetc() function to read one character at a time. The loop should terminate when fgetc() returns the EOF (End-Of-File) constant.
+ Show Solution
Explanation:
- The file is read using the
fgetc()function, which returns the next character from the file stream as anint. We use anintto store the return value because it must be able to hold theEOFvalue (which is outside the range of a standardchar). - Inside the
whileloop, as long as the returned value is not equal toEOF, the character counter (char_count) is incremented.
Exercise 5: Count Lines in a File
Practice Problem: Read a any text file (e.g., data.txt updated in Exercise 3) and count the total number of lines in it. (A line is typically counted by the presence of the newline character \n).
Expected Output:
Total number of lines in the file: 4
+ Hint
Similar to the character counting exercise, use fgetc(). Inside the loop, check if the character read is equal to the newline character ('\n'). Increment a counter each time a newline is found.
+ Show Solution
Explanation:
This exercise uses fgetc() to iterate through the file.
- The program specifically looks for the newline character (
'\n'); every time this character is found, theline_countis incremented. - A key complexity in line counting is handling the last line of the file. If a file ends with actual content but without a final newline character, the last line won’t be counted.
- The robust solution often involves a conditional check after the loop to see if the file contained any characters at all and if the last character read was not a newline.
Exercise 6: Count Words in a File
Practice Problem: Read a any text file (e.g., data.txt updated in Exercise 3) and count the total number of words. Assume words are separated by spaces, tabs, or newlines.
Expected Output:
Total number of words in the file: 12
+ Hint
This is a state-machine problem. Define a state variable (IN or OUT of a word). When you encounter a non-delimiter character and the state is OUT, a new word has started, so increment the word count and change the state to IN.
+ Show Solution
Explanation:
- This program uses a simple state machine to accurately count words. The
statevariable tracks whether the program is currentlyINa word orOUTof a word (i.e., at a delimiter). - The
isspace()function checks for any whitespace character (space, tab, newline, etc.). A word is counted only when the program transitions from theOUTstate to theINstate, meaning a non-delimiter character was just encountered after one or more delimiters. This correctly handles multiple spaces between words.
Exercise 7: Copy File
Practice Problem: Create a program to copy the entire content of a source file (data.txt updated in Exercise 3) to a destination file (destination.txt).
Expected Output:
destination.txt
Name: Alice Johnson
Age: 30 years
Date Appended: 2025-10-15
Status: Successfully appended.
+ Hint
You need two file pointers: one for the source file opened in read mode ("r") and one for the destination file opened in write mode ("w"). Read data from the source using fgetc() and write it immediately to the destination using fputc().
+ Show Solution
Explanation:
- The program utilizes two separate file pointers. It reads one character at a time from the source file using
fgetc()until theEOFis reached. For every character read,fputc()is immediately used to write that same character to the destination file stream. - This is the most fundamental and robust method for binary or text file copying in C. Crucially, the program includes error checking for both file operations and ensures that the source file is closed even if the destination file fails to open.
Exercise 8: Display a Specific Line of a File
Practice Problem: Prompt the user to enter a line number N. Read a text file (e.g. data.txt updated in Exercise 3) and display the content of the Nth line on the console.
Expected Output:
Enter the line number to display: 2
--- Line 2 ---
Age: 30 years
--------------
+ Hint
Open the file in read mode. Use a loop and fgets() to read lines sequentially. Keep a line counter. Only print the line when the counter matches the user’s input N.
+ Show Solution
Explanation:
- After getting the desired
target_linefrom the user, the program reads the file line by line usingfgets(). - A
current_linecounter is incremented for every line successfully read. Whencurrent_lineequalstarget_line, the content of thebufferis printed, the file is closed, and the program exits. - If the loop finishes without the condition being met, it means the requested line number was greater than the total number of lines in the file.
Exercise 9: Write a Series of Numbers to a File
Practice Problem: Write a C program to write a sequence of integers from 1 to 10 to a new file named numbers.txt, ensuring each number is on a separate line.
Expected Output:
numbers.txt
1
2
3
...
10
+ Hint
Use a simple for loop that iterates from 1 to 10. Inside the loop, use fprintf() with the format specifier "%d\n" to write the integer followed by a newline character.
+ Show Solution
Explanation:
- The file is opened in write mode (
"w"). A standardforloop is used to generate the numbers 1 through 10. - The key function is
fprintf(fp, "%d\n", i). The\n(newline character) appended to the format string is crucial, as it ensures that each subsequent number is written starting on a new line in the text file, fulfilling the problem requirement.
Exercise 10: Read Numbers and Calculate Sum
Practice Problem: Read the sequence of integers from the file numbers.txt (created in Exercise 9) and calculate and display their total sum.
Expected Output:
Reading numbers from file and calculating sum...
Successfully processed all numbers.
The total sum of the numbers is: 55
+ Hint
Open the file in read mode ("r"). Use a while loop combined with fscanf() to read the numbers. The loop should continue as long as fscanf() successfully reads an integer (i.e., returns 1).
+ Show Solution
Explanation:
- The file is opened in read mode.
- The core of the program is the
whileloop condition:(read_count = fscanf(fp, "%d", &number)) == 1. Thefscanf()function attempts to read an integer from the file and store it at the address of thenumbervariable. It returns the count of items successfully matched and assigned. - As long as it returns
1, an integer was read and assigned, and it is added to thesum. Whenfscanf()reaches the end of the file or encounters non-numeric data, it will return a value other than1, terminating the loop.
Exercise 11: Uppercase Conversion
Practice Problem: Read the content of any existing text file (e.g. data.txt updated in Exercise 3) and write its content to a new file (output_upper.txt), converting all lowercase letters to uppercase letters. All other characters (numbers, symbols, spaces) should remain unchanged.
Expected Output:
output_upper.txt
NAME: ALICE JOHNSON
AGE: 30 YEARS
DATE APPENDED: 2025-10-15
STATUS: SUCCESSFULLY APPENDED.
+ Hint
Use two file pointers, one for reading ("r") and one for writing ("w"). Read the file character by character using fgetc(). Use the standard C library function toupper() (from <ctype.h>) to check and convert the character before writing it using fputc().
+ Show Solution
Explanation:
The program reads the input file character by character using fgetc(). The retrieved character is passed directly to the toupper() function.
If the character is a lowercase letter, toupper() returns its uppercase equivalent; otherwise, it returns the character itself unchanged. This result is immediately written to the output file using fputc(). This ensures a perfect copy with only the casing modified.
Exercise 12: Remove Vowels from a File
Practice Problem: Read a any text file (e.g. data.txt updated in Exercise 3) and write its content to a new file (no_vowels.txt), excluding all lowercase and uppercase vowels (A, E, I, O, U, a, e, i, o, u).
Expected Output:
no_vowels.txt
Nm: lc Jhnsn
g: 30 yrs
Dt ppndd: 2025-10-15
Stts: Sccssflly ppndd.
+ Hint
- Use
fgetc()to read characters. - Within the loop, use a series of logical OR (
||) comparisons to check if the current character is a vowel (checking both cases). If it is not a vowel, write the character to the output file usingfputc().
+ Show Solution
Explanation:
- This program defines a helper function,
is_vowel(), for clarity. - The main loop reads the file character by character. Inside the loop, the condition
!is_vowel(character)checks if the character is not a vowel (both upper and lower case). Only when this condition is true is the character written to the output file usingfputc(). - All vowel characters are simply skipped, effectively removing them from the copied content.
Exercise 13: Search for a Word
Practice Problem: Prompt the user to enter a filename (you can refer data.txt updated in Exercise 3) and a target word. Read the specified file and report the total number of times that target word appears in the file. Assume the file contains words separated by spaces or newlines.
Expected Output:
Enter the filename: data.txt
Enter the word to search: Johnson
The word 'Johnson' appears 1 times in 'data.txt'.
+ Hint
- Use
fscanf()within awhileloop to read the file word-by-word into a temporary string buffer. - Use the C string function
strcmp()(from<string.h>) to compare the word read from the file with the user’s target word.
+ Show Solution
Explanation:
- The program takes the filename and the
target_wordfrom the user. It then usesfscanf(fp, "%s", file_word)inside a loop. The%sformat specifier tellsfscanf()to read a string (a “word”) until it encounters any whitespace (space, tab, or newline) and stores it infile_word. - This elegantly handles word separation. The
strcmp()function performs a case-sensitive comparison. Whenstrcmp()returns0, the words match, and the counter is incremented.
Exercise 14: Replace Word in a File
Practice Problem: Read a any text file (you can refer data.txt updated in exercise 3) and replace all occurrences of a specific word (e.g., “the”) with a new word (e.g., “a”) and save the result to a new file (modified.txt).
Given:
sample.txt
The mouse that the cat hit that the dog bit that the fly landed on ran away
Expected Output:
The mouse that a cat hit that a dog bit that a fly landed on ran away
+ Hint
- Similar to Exercise 13, read the file word by word using
fscanf(). - Compare the read word with the word to be replaced. If they match, write the new word to the output file; otherwise, write the original word. Remember to add a space after each word written.
+ Show Solution
Explanation:
- The program reads
sample.txtword by word usingfscanf(). - Inside the loop, it checks if
file_wordis equal to the predefinedOLD_WORDusingstrcmp(). If they are equal, theNEW_WORDis written tomodified.txtusingfprintf(); - otherwise, the original
file_wordis written. A space character is appended after every word written ("%s ") to maintain separation, sincefscanf("%s", ...)consumes the delimiter but doesn’t store it.
Exercise 15: Display File Content in Reverse
Practice Problem: Read a any text file and display its entire content character by character in reverse order (starting from the last character and ending with the first).
Given:
reverse_me.txt
Name: Alice Johnson
Age: 30 years
Expected Output:
Reversed content:
sraey 03 :egA
nosnhoJ ecilA :emaN
+ Hint
- This requires moving the file pointer.
- Use
fseek()withSEEK_ENDto find the end of the file, andftell()to get the total file size. Then, loop backward from the end, usingfseek()withSEEK_SETto position the pointer one character back, andfgetc()to read the character.
+ Show Solution
Explanation:
- This exercise heavily relies on
fseek()andftell()for random access (non-sequential I/O). - First,
fseek(fp, 0, SEEK_END)positions the pointer at the end of the file, andftell()retrieves this position, which is the file’s size in bytes. - The
forloop iterates backward fromsize - 1(the index of the last character) to0(the first character). - Inside the loop,
fseek(fp, i, SEEK_SET)moves the file pointer to the ith byte from the beginning, andfgetc()reads the character at that exact location.
Exercise 16: Merge Two Files
Practice Problem: Read the content of two separate files (file1.txt and file2.txt) and write their combined content sequentially into a third file (merged.txt).
Given:
file1.txt
This is file 1
file2.txt
This is file2
Expected Output:
merged.txt
This is file 1
--- End of File 1 ---
This is file 2
+ Hint
- Open three file pointers:
fp1andfp2for reading ("r") andfp_outfor writing ("w"). - Copy the entire content of
fp1tofp_out, and then copy the entire content offp2tofp_out. Use a character-by-character copy loop (withfgetc()andfputc()) for both source files.
+ Show Solution
Explanation:
- The program defines a reusable
copy_filefunction to handle the character-by-character transfer logic. It opens both source files in read mode and the destination file in write mode. - It calls
copy_filefirst withfp1as the source, and then withfp2as the source. Since the destination file pointer (fp_out) remains open, the second call tocopy_fileautomatically appends the content offile2.txtimmediately after the content offile1.txt.
Exercise 17: Tab to Spaces Conversion
Practice Problem: Read a file (tabbed.txt) and replace every tab character (\t) with exactly four space characters (), saving the result to a new file (spaced.txt).
Expected Output:
Tabs replaced with 4 spaces. Saved to 'spaced.txt'.
+ Hint
- Read the file character by character using
fgetc(). - Use a conditional check to see if the character is a tab (
'\t'). If it is, use a loop to write the required four spaces to the output file usingfputc(). - If it is any other character, write the original character directly.
+ Show Solution
Explanation:
The program reads the input file using fgetc(). Inside the loop, it checks if the character is equal to the tab escape sequence ('\t'). If it is, a small for loop executes, writing the space character (' ') to the output file exactly four times using fputc().
If the character is not a tab, it is written to the output file unmodified.
Exercise 18: Simple Encryption (Caesar Cipher)
Practice Problem: Implement a simple Caesar cipher (shift by N=3) to encrypt the contents of an input file (plain.txt) and write the result to an output file (encrypted.txt). Only alphabet characters (A-Z, a-z) should be shifted; all others should be written as is.
Given:
plain.txt
Name: Alice Johnson
Age: 30 years
Expected Output:
encrypted.txt
Qdph: Dolfh Mrkqvrq
Djh: 30 bhduv
+ Hint
- Use
fgetc()to read the file character by character. - If the character is a letter, check its case. Apply the formula:
encrypted_char = (original_char - base + shift) % 26 + base. The base is'a'for lowercase and'A'for uppercase. - Non-alphabetic characters should bypass the encryption logic.
+ Show Solution
Explanation:
- The program reads the file character by character. It uses
isalpha()to check if the character is a letter. If it is, it determines thebase('a'or'A') usingislower()orisupper(). - The core Caesar cipher logic uses modular arithmetic:
(character - base)converts the letter to an index 0−25. Adding theSHIFTand taking the result modulo 26 wraps the index around the alphabet. - Finally, adding
baseconverts the index back into the shifted ASCII character. Non-alphabetic characters are written directly without modification.
Exercise 19: Simple Decryption (Caesar Cipher)
Practice Problem: Implement the corresponding decryption for the file (encrypted.txt) created in Exercise 19. The program should read the encrypted file and write the original plaintext to a new file (decrypted.txt).
Expected Output:
decrypted.txt
Name: Alice Johnson
Age: 30 years
+ Hint
- The decryption is the reverse of the encryption.
- Since the encryption used a positive shift, the decryption requires an equivalent negative shift.
- The formula becomes:
decrypted_char = (original_char - base - shift) % 26 + base. - Be careful with negative results of the modulo operator in C; a safer calculation is
(original_char - base - shift + 26) % 26 + base.
+ Show Solution
Explanation:
- The structure is identical to the encryption program, but the mathematical operation is reversed.
- Decryption requires subtracting the
SHIFTvalue. In C, the result of the modulo operator (%) can be negative if the numerator is negative. - To correctly handle the alphabet wrap-around (e.g., decrypting ‘C’ with a shift of 3 should result in ‘Z’), the expression includes an extra +26 before the final modulo operation:
(index - SHIFT + 26) % 26. This guarantees a positive result within the 0−25 range, ensuring the character correctly wraps backward from ‘a’ to ‘z’ or ‘A’ to ‘Z’.
Exercise 20: Write Student Record (Binary)
Practice Problem: Define a C struct Student containing name (string), roll_number (int), and marks (float). Write a single student’s record received from the user to a binary file named students.dat using fwrite().
Given:
// Student structure
struct Student {
char name[50];
int roll_number;
float marks;
};Code language: C++ (cpp)
Expected Output:
Enter Student Name: Jessa
Enter Roll Number: 25
Enter Marks: 85
Student record successfully written to students.dat (Binary).
+ Hint
- Open the file in write-binary mode (
"wb"). - Use the
sizeof()operator to determine the exact size of thestruct Studentto be written. - The function
fwrite()is essential here, as it writes the raw memory representation of the structure directly to the file.
+ Show Solution
Explanation:
The file is opened in binary write mode ("wb"). The fwrite() function is used to write data. Its arguments are:
- The address of the data block to write (
&s). - The size of each item to write (
sizeof(struct Student)). - The number of items to write (1).
- The file pointer (
fp). This approach writes the structure as a continuous block of raw bytes, making it a highly efficient way to store structured, non-textual data.
Exercise 21: Read Student Record (Binary)
Practice Problem: Read the single student record from the binary file students.dat (created in Exercise 21) using fread() and display the student’s details on the console.
Given:
/ Re-define the structure
struct Student {
char name[50];
int roll_number;
float marks;
};Code language: C++ (cpp)
Expected Output:
--- Student Record Read ---
Name: Jessa
Roll Number: 25
Marks: 85.00
---------------------------
+ Hint
- Open the file in read-binary mode (
"rb"). - Use a
struct Studentvariable to hold the data read from the file. - The function
fread()should read exactly one item ofsizeof(struct Student).
+ Show Solution
Explanation:
- The file is opened in binary read mode (
"rb"). - The
fread()function is the counterpart tofwrite(). It attempts to read data into the memory location provided (&s). Since the function returns the number of items successfully read, checking if the return value is1confirms that a completeStudentrecord was successfully retrieved from the binary file.
Exercise 22: Multiple Records (Binary)
Practice Problem: Write the records of N students (where N is input by the user) to a new binary file, class.dat.
Given:
struct Student {
char name[50];
int roll_number;
float marks;
};Code language: C++ (cpp)
Expected Output:
Enter the number of students to record: 2
--- Student 1 ---
Name: Jessa
Roll Number: 25
Marks: 85
--- Student 2 ---
Name: Sam
Roll Number: 26
Marks: 88
2 student records successfully written to class.dat.
+ Hint
Use an array of struct Student or use a for loop to repeatedly prompt the user for input and call fwrite() for each student record.
+ Show Solution
Explanation:
- The program uses a standard
forloop to iterate N times. - In each iteration, it prompts the user for the three fields of the
Studentstructure. Once the fields are populated,fwrite()is called, writing one complete block ofsizeof(struct Student)data to the file. - Since the file is open in binary mode, the records are stored contiguously, one after the other.
Exercise 23: Search and Display Record (Binary)
Practice Problem: Read the multiple student records from the binary file class.dat (created in Exercise 22) and allow the user to search for a student by their roll number, displaying their full details if found.
Given:
struct Student {
char name[50];
int roll_number;
float marks;
};Code language: C++ (cpp)
Expected Output:
Enter the Roll Number to search: 26
--- Record Found ---
Name: Sam
Roll Number: 26
Marks: 88.00
--------------------
+ Hint
- Read records sequentially using a
whileloop withfread(). - Check the return value of
fread()to know when EOF is reached. - Inside the loop, compare the
roll_numberin the structure with the user’s target roll number.
+ Show Solution
Explanation:
- The file is opened in binary read mode.
- The
whileloop repeatedly callsfread()to load the next record from the file into thesvariable. - If
fread()successfully reads a record (returns 1), the program compares the student’sroll_numberwith thetarget_roll. If they match, the details are printed, thefoundflag is set, and thebreakstatement exits the loop, improving efficiency.
Exercise 24: Update Record (Binary)
Practice Problem: Allow the user to update the marks of a specific student identified by their roll number in the binary file class.dat (created in Exercise 22). This requires using fseek() for direct access.
Given:
struct Student {
char name[50];
int roll_number;
float marks;
};Code language: C++ (cpp)
Expected Output:
Enter Roll Number to update marks for: 26
Enter new Marks: 92
Marks for Sam (Roll 26) successfully updated.
+ Hint
- Open the file in read/write binary mode (
"r+b"). - Search for the record sequentially. Once the record is found, use
fseek()to move the file pointer back to the start of the found record. - Update the record’s data in memory, and then use
fwrite()to overwrite the old data at that exact position.
+ Show Solution
Explanation:
- The file is opened in
"r+b"mode, allowing both reading and writing (overwriting) of binary data. - The program reads records until the
target_rollis found. Crucially, after a successfulfread(), the file pointer is at the end of the record that was just read. - To overwrite this record,
fseek(fp, -record_size, SEEK_CUR)moves the pointer backward by the size of one record (record_size) from the current position (SEEK_CUR). - The in-memory structure
sis updated, andfwrite()then overwrites the old record’s data with the new data at the repositioned pointer.
Exercise 25: Determine File Size
Practice Problem: Write a program to find and display the size of a given file (e.g., class.dat created in Exercise 22) in bytes.
Expected Output:
The size of 'class.dat' is: 120 bytes
+ Hint
- Open the file in read mode (
"r"or"rb"). - Use
fseek()to move the file pointer to the end of the file, starting from the beginning (SEEK_END). - Then, use
ftell()to get the current position of the pointer, which will represent the total size of the file in bytes.
+ Show Solution
Explanation:
This is the standard C method for finding file size.
- The call
fseek(fp, 0, SEEK_END)moves the file pointer zero bytes relative to the end of the file, placing it right after the last byte. - The function
ftell(fp)then returns the current absolute position of the pointer, measured from the beginning of the file. Since the pointer is at the end, the returned value is exactly the total size of the file in bytes.
Exercise 26: Robust Error Checking
Practice Problem: Write a program that attempts to open a non-existent file (missing.txt) for reading. Implement robust error checking using the NULL check and display a detailed system error message using perror() if the open operation fails.
Expected Output:
Attempting to open the file 'missing.txt'...
File Open Error: No such file or directory
Program will now exit.
+ Hint
Attempt to open a file that you know does not exist in read mode ("r"). Check the returned FILE pointer against NULL. If it is NULL, call perror() with a descriptive string.
+ Show Solution
Explanation:
- When
fopen()fails (e.g., file not found, permission denied), it returns aNULLpointer and sets a global integer variable namederrnoto an appropriate error code. - The program checks for the
NULLreturn. Theperror()function is the standard C mechanism for reporting such errors. It prints the string argument you pass (“File Open Error” in this case), followed by a colon and the system’s human-readable description of the error code stored inerrno. This provides much more informative output than a simple “File Error” message

Leave a Reply