C Programming: File I/O Examples 📝

beginner
14 min

C Programming: File I/O Examples 📝

Welcome to our comprehensive guide on C File I/O! In this tutorial, we'll explore how to read and write files in C, a fundamental skill for any C programmer. 🎯

Understanding File I/O 📝

File Input/Output (I/O) is a method of communicating with files to read data from them or write data to them. In C, we use standard file streams stdin, stdout, and stderr for interacting with the keyboard and console. However, for reading and writing files, we need to open them first.

Opening Files 🎯

To open a file in C, we use the fopen() function. This function takes two arguments: the name of the file and the mode in which we want to open it. The syntax is as follows:

c
FILE *fp = fopen("filename", "mode");

Here, fp is a pointer to a FILE structure, which holds information about the opened file. If the file is opened successfully, fp will contain a valid FILE pointer; otherwise, it will be NULL.

Common Modes 📝

  • "r": Open the file in read mode. The file pointer is positioned at the beginning of the file.
  • "w": Open the file in write mode. If the file exists, it is truncated; if it doesn't exist, it is created. The file pointer is positioned at the beginning of the file.
  • "a": Open the file in append mode. If the file exists, the file pointer is positioned at the end of the file; if it doesn't exist, it is created.

Reading from Files 🎯

To read data from a file, we use the fread() function. This function reads a certain number of bytes from the file pointed by fp and stores them in the memory pointed by the first argument. The syntax is as follows:

c
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream);

Here, ptr is a pointer to the memory where the read data will be stored, size is the size of each element in bytes, nmemb is the number of elements to read, and stream is the FILE pointer of the opened file.

Writing to Files 🎯

To write data to a file, we use the fwrite() function. This function writes a certain number of bytes from the memory pointed by ptr to the file pointed by fp. The syntax is as follows:

c
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream);

Here, ptr is a pointer to the memory containing the data to be written, size is the size of each element in bytes, nmemb is the number of elements to write, and stream is the FILE pointer of the opened file.

Closing Files 🎯

After we're done with a file, we should close it using the fclose() function. This function flushes the file buffer and closes the file pointed by fp. The syntax is as follows:

c
int fclose(FILE *stream);

Here, stream is the FILE pointer of the opened file.

Example: Reading from a File 🎯

Let's read the contents of a file called data.txt and print them to the console.

c
#include <stdio.h> int main() { FILE *fp = fopen("data.txt", "r"); if (fp == NULL) { printf("Error opening file.\n"); return 1; } char data[100]; size_t bytesRead = fread(data, sizeof(char), sizeof(data), fp); fclose(fp); printf("File contents:\n%s\n", data); return 0; }

Example: Writing to a File 🎯

Now, let's write some data to a file called output.txt.

c
#include <stdio.h> int main() { FILE *fp = fopen("output.txt", "w"); if (fp == NULL) { printf("Error opening file.\n"); return 1; } char *data = "Hello, World!"; size_t bytesWritten = fwrite(data, sizeof(char), strlen(data), fp); fclose(fp); printf("%d bytes written to file.\n", (int)bytesWritten); return 0; }
Quick Quiz
Question 1 of 1

What does the `fopen()` function do in C?

Quick Quiz
Question 1 of 1

What is the purpose of the `fread()` function in C?

Quick Quiz
Question 1 of 1

What is the purpose of the `fwrite()` function in C?

Quick Quiz
Question 1 of 1

What does the `fclose()` function do in C?