📖 Scenario: You are creating a simple program to write and read text from a file using file pointers in C. This is like writing a note on a piece of paper and then reading it back later.
🎯 Goal: Build a C program that opens a file, writes a message, moves the file pointer to the start, reads the message back, and prints it on the screen.
📋 What You'll Learn
Create a file pointer variable
Open a file named example.txt in write mode
Write the exact string "Hello, File Pointer!" to the file
Use fseek to move the file pointer to the beginning of the file
Read the string back from the file into a character array
Print the read string using printf
Close the file properly
💡 Why This Matters
🌍 Real World
File pointers let programs read and write files, which is useful for saving data, logs, or configuration settings.
💼 Career
Understanding file handling is important for software developers working with data storage, system programming, and application development.
Progress0 / 4 steps
1
Create a file pointer and open a file
Declare a file pointer variable called fp and open a file named example.txt in write mode using fopen.
C
Hint
Use FILE *fp to declare the file pointer and fopen("example.txt", "w") to open the file for writing.
2
Write a string to the file
Use fprintf to write the exact string "Hello, File Pointer!" to the file using the file pointer fp.
C
Hint
Use fprintf(fp, "Hello, File Pointer!") to write the string to the file.
3
Move file pointer and read the string
Close the file fp, then reopen example.txt in read mode. Use fseek to move the file pointer to the beginning of the file. Read the string into a character array called buffer of size 50 using fgets.
C
Hint
Remember to close the file before reopening it in read mode. Use fseek(fp, 0, SEEK_SET) to move to the start. Use fgets(buffer, 50, fp) to read the string.
4
Print the read string and close the file
Use printf to print the string stored in buffer. Then close the file pointer fp.
C
Hint
Use printf("%s", buffer) to print the string and fclose(fp) to close the file.
Practice
(1/5)
1. What does a file pointer in C represent?
easy
A. A variable that stores file contents
B. A reference to a file opened in the program
C. A function to read files
D. A type of file format
Solution
Step 1: Understand what a file pointer is
A file pointer is a special pointer that points to a file stream opened by the program.
Step 2: Differentiate from other options
It is not the file contents or a function, but a reference to the file for reading or writing.
Final Answer:
A reference to a file opened in the program -> Option B
Quick Check:
File pointer = Reference to opened file [OK]
Hint: File pointer points to file stream, not contents [OK]
Common Mistakes:
Thinking file pointer stores file data
Confusing file pointer with file reading function
Assuming file pointer is a file format
2. Which of the following is the correct way to open a file for reading in C?
easy
A. FILE *fp = fopen("file.txt", "r");
B. FILE fp = fopen("file.txt", "r");
C. fopen FILE *fp = "file.txt", "r";
D. FILE *fp = open("file.txt", "r");
Solution
Step 1: Recall fopen syntax
The fopen function returns a pointer to FILE, so the variable must be FILE *fp.
Step 2: Check each option
FILE *fp = fopen("file.txt", "r"); correctly declares FILE *fp and calls fopen with filename and mode "r".