Skip to content

Instantly share code, notes, and snippets.

@024x
Last active August 15, 2023 03:28
Show Gist options
  • Select an option

  • Save 024x/426c8e7b7639cf35469540c275d28886 to your computer and use it in GitHub Desktop.

Select an option

Save 024x/426c8e7b7639cf35469540c275d28886 to your computer and use it in GitHub Desktop.
Advanced File Operations in C - A gist that explains how to perform advanced file operations such as copying, renaming, deleting, and appending files in C language with examples. - Satyendra

Advanced File Operations in C

Binary File I/O

Main Issue: Understanding binary file I/O in C programming and how to perform operations like reading and writing binary data to files.

Bullet Points:

  • In addition to text files, C also allows you to work with binary files, which store data in its raw binary format.
  • Binary files are useful for storing complex data structures, such as arrays, structures, and objects, directly to disk.
  • Binary file I/O involves reading or writing data in binary format, without any text conversion.
  • To work with binary files, you need to open the file in binary mode using the "b" flag in the file mode specifier.
  • Functions like fread() and fwrite() are used for reading from and writing to binary files, respectively.
  • It's important to handle the binary data carefully and ensure proper structuring to avoid data corruption or misinterpretation.

Keywords:

Step 1: Opening Binary Files When working with binary files, you need to open the file in binary mode. This is done by appending the "b" character to the mode specifier when using functions like fopen(). Here's an example:

FILE* file = fopen("data.bin", "rb");

In this example, "data.bin" is the name of the binary file, and "rb" indicates that the file is opened in read mode with binary access.

Step 2: Reading Binary Data To read binary data from a file, you can use the fread() function. It allows you to specify the size and number of elements to read from the file. Here's an example that reads an array of integers from a binary file:

int numbers[5];
fread(numbers, sizeof(int), 5, file);

In this example, numbers is an array of integers, sizeof(int) specifies the size of each element, 5 indicates the number of elements to read, and file is the file pointer.

Step 3: Writing Binary Data To write binary data to a file, you can use the fwrite() function. It takes the data to be written, the size of each element, the number of elements, and the file pointer. Here's an example that writes an array of integers to a binary file:

int numbers[5] = {1, 2, 3, 4, 5};
fwrite(numbers, sizeof(int), 5, file);

In this example, numbers is an array of integers to be written, sizeof(int) specifies the size of each element, 5 indicates the number of elements, and file is the file pointer.

Step 4: Seeking in Binary Files Seeking allows you to move the file pointer to a specific position within the file. This is useful for reading or writing data at a specific location. You can use the fseek() function to perform seeking operations. Here's an example:

fseek(file, offset, SEEK_SET);

In this example, file is the file pointer, offset is the number of bytes to move, and SEEK_SET is the position from where the offset is measured (in this case, the beginning of the file).

Step 5: Practice Programs To practice binary file I/O, you can create programs that read and write complex data structures like arrays or structures to binary files. Here's an example that demonstrates reading and writing an array of structures to a binary file:

#include <stdio.h>

struct Person {
  char name[20];
  int age;
};

int main() {
  struct Person people[3];

  // Writing data to a binary file
  FILE* file = fopen("people.bin", "wb");
  fwrite(people, sizeof(struct Person), 3, file);
  fclose(file);

  // Reading data from a binary file
  file = fopen("people.bin", "rb");
  fread(people, sizeof(struct Person), 3, file);
  fclose(file);

  return 0;
}

In this example, we define a structure called Person with two members: name and age. We create an array of Person structures named people. The program writes the array of structures to a binary file named "people.bin" and then reads it back from the file.

By following these steps and experimenting with the provided examples, you'll gain a better understanding of binary file I/O and advanced file operations in C.

Sources:

Here are some sources that cover binary file I/O and advanced file operations in C:

  1. Binary file I/O in C (GeeksforGeeks)
  2. Binary File I/O in C (Tutorialspoint)
  3. Binary File Handling in C (Cprogramming.com)
  4. C File I/O (Learn-C.org)
  5. C File Handling (Programiz)
  6. File Handling in C (Studytonight)

These sources provide explanations, examples, and tutorials on binary file I/O and advanced file operations in C. They will help you gain a more in-depth understanding of the concepts and practical usage of these operations.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment