Sudo Security

Sudo Security

Share

Code>_Break>_Secure>_Repeat

23/08/2025

The fopen() function in C is used to open a file and return a file pointer that lets you perform operations like reading, writing, or appending data.
FILE *fp = fopen("fprintf.c", "a+");
filename: The name (or path) of the file you want to open.

mode: The mode in which you want to open the file (r, w, a, etc.).

Return: A pointer to FILE (often called fp) if successful, otherwise NULL if the operation fails.

đź“– Modes in fopen()
Here are the most common modes you’ll use:

Mode Description
"r" Opens an existing file for reading. Fails if the file doesn’t exist.
"w" Creates a new file for writing (or overwrites if it exists).
"a" Opens a file for appending (creates it if it doesn’t exist).
"r+" Opens a file for both reading and writing.
"w+" Creates a new file for reading and writing (overwrites if exists).
"a+" Opens a file for reading and appending.

23/08/2025

Mastering fopen() in C: A Beginner-Friendly Guide to File Handling
If you’re learning C, one of the most powerful concepts you’ll encounter is file handling. And at the center of it all is the fopen() function. In this post, we’ll break down what fopen() does, how to use it correctly, and show some practical fopen in C examples you can apply right away.

📌 What is fopen() in C?
The fopen() function in C is used to open a file and return a file pointer that lets you perform operations like reading, writing, or appending data.

It’s declared in the stdio.h header:

FILE *fopen("example.c", "r");
filename: The name (or path) of the file you want to open.

mode: The mode in which you want to open the file (r, w, a, etc.).

Return: A pointer to FILE (often called fp) if successful, otherwise NULL if the operation fails.

đź“– Modes in fopen()
Here are the most common modes you’ll use:

Mode Description
"r" Opens an existing file for reading. Fails if the file doesn’t exist.
"w" Creates a new file for writing (or overwrites if it exists).
"a" Opens a file for appending (creates it if it doesn’t exist).
"r+" Opens a file for both reading and writing.
"w+" Creates a new file for reading and writing (overwrites if exists).
"a+" Opens a file for reading and appending.
âś… Example 1: Opening a File for Reading


int main() {
FILE *fp;
fp = fopen("example.c", "r");

if (fp == NULL) {
printf("Error: Could not open file.\n");
return 1;
}

printf("File opened successfully!\n");
fclose(fp);
return 0;
}
Explanation:

If example.c exists, it opens successfully.

If not, fopen() returns NULL, and we print an error message.

âś… Example 2: Writing to a File


int main() {
FILE *fp;
fp = fopen("output.c", "w");

if (fp == NULL) {
printf("Error: Could not create file.\n");
return 1;
}

fprintf(fp, "Hello, world!\n");
fclose(fp);

printf("Data written to file successfully.\n");
return 0;
}
Explanation:

"w" mode creates output.c if it doesn’t exist.

fprintf() writes data into the file.

Always use fclose(fp) to save and close the file.

âś… Example 3: Appending Data to a File


int main() {
FILE *fp;
fp = fopen("log.c", "a");

if (fp == NULL) {
printf("Error: Could not open file.\n");
return 1;
}

fprintf(fp, "New log entry.\n");
fclose(fp);

printf("Log entry added successfully.\n");
return 0;
}
Explanation:

"a" mode adds data to the end of the file without deleting old content.

⚠️ Common Mistakes with fopen()
❌ Forgetting to check if fp == NULL → leads to runtime crashes.

❌ Using wrong mode (like "r" on a file that doesn’t exist).

❌ Forgetting fclose(fp) → can cause data loss or corruption.

🔑 Key Takeaways
Use fopen() for file handling in C: reading, writing, or appending.

Always check if fopen() returned NULL before using the file.

Don’t forget to call fclose() when you’re done.

🚀 Final Thoughts
Mastering fopen() is your first step toward mastering file handling in C. Whether you’re building a student record system, writing logs, or processing text files, knowing how to open, write, and append files safely is a skill you’ll use everywhere.

23/07/2025

Check weather in CMD with curl wrrt.in/location commands.

05/07/2025

Linux Performance Check with htop, btop and top.
📊 In this short video, we compare the 3 most popular Linux system monitoring commands: top, htop, and btop. Whether you're a system admin, programmer, or just a Linux newbie, knowing how to monitor system performance is crucial!

▶ htop – A more colorful, interactive version of top with extra features.
▶ btop – A beautiful, modern system monitor with graphs and mouse support!
▶ top – The classic task manager that comes pre-installed on almost every Linux system.

Learn which tool is best for your workflow and get the most out of your Linux terminal.

đź’ˇ Subscribe for more Linux tutorials, command breakdowns, and productivity hacks!

đź’¬ Comment which one you use: top, htop, or btop?

20/06/2025

Which one is a programming language?

20/06/2025

With Life at Google – I just got recognised as one of their top fans! 🎉

17/06/2025

Which one is a Programming language?

Want your school to be the top-listed School/college in Dhaka?

Click here to claim your Sponsored Listing.

Location

Category

Address

Changao
Dhaka
1340