Introduction:
In the realm of C programming, nesting structures and file input/output (I/O) are advanced yet powerful techniques that allow developers to create complex data structures and work with external files. In this blog post, we’ll delve into the world of nesting structures, where structures are defined within other structures, and file I/O, where we can read from and write to files. We’ll explore their definitions, usage, examples, and practical applications.

Nesting Structures:

  1. Definition of Nested Structures:
   struct Address {
       char street[50];
       char city[50];
       int postalCode;
   };

   struct Employee {
       int empId;
       char name[50];
       float salary;
       struct Address empAddress; // Nested Address structure
   };
  1. Accessing Nested Structure Members:
   struct Employee emp1 = {101, "Alice", 50000.0, {"123 Main St", "Anytown", 12345}};
   printf("Employee ID: %d\n", emp1.empId);
   printf("Employee Name: %s\n", emp1.name);
   printf("Employee Address: %s, %s, %d\n", emp1.empAddress.street, emp1.empAddress.city, emp1.empAddress.postalCode);

File Input/Output (I/O) in C:

  1. Opening and Closing Files:
   FILE *filePointer; // File pointer
   filePointer = fopen("data.txt", "w"); // Open file in write mode
   // Perform operations on file
   fclose(filePointer); // Close file
  1. Writing to a File:
   FILE *outputFile;
   outputFile = fopen("output.txt", "w");

   fprintf(outputFile, "Hello, World!\n");
   fprintf(outputFile, "This is a line written to the file.\n");

   fclose(outputFile);
  1. Reading from a File:
   FILE *inputFile;
   inputFile = fopen("input.txt", "r");

   char buffer[100];
   while (fgets(buffer, sizeof(buffer), inputFile) != NULL) {
       printf("%s", buffer);
   }

   fclose(inputFile);

Practical Example: Employee Database with File I/O

Let’s create a practical example of an employee database that stores employee information in a file using nesting structures and file I/O.

#include <stdio.h>
#include <string.h>

struct Address {
    char street[50];
    char city[50];
    int postalCode;
};

struct Employee {
    int empId;
    char name[50];
    float salary;
    struct Address empAddress;
};

void writeEmployeeToFile(struct Employee emp) {
    FILE *filePointer;
    filePointer = fopen("employee_data.txt", "a");

    fprintf(filePointer, "Employee ID: %d\n", emp.empId);
    fprintf(filePointer, "Name: %s\n", emp.name);
    fprintf(filePointer, "Salary: %.2f\n", emp.salary);
    fprintf(filePointer, "Address: %s, %s, %d\n", emp.empAddress.street, emp.empAddress.city, emp.empAddress.postalCode);
    fprintf(filePointer, "--------------------------------\n");

    fclose(filePointer);
}

int main() {
    struct Employee emp1 = {101, "Alice", 50000.0, {"123 Main St", "Anytown", 12345}};
    struct Employee emp2 = {102, "Bob", 60000.0, {"456 Oak Ave", "Sometown", 54321}};

    writeEmployeeToFile(emp1);
    writeEmployeeToFile(emp2);

    printf("Employee data has been written to file.\n");

    return 0;
}

Conclusion:
Nesting structures and file I/O are powerful techniques in C programming that enable developers to create hierarchical data structures and interact with external files. By nesting structures, we can create complex data types that represent real-world entities with multiple attributes. File I/O allows us to read from and write to files, enabling data persistence and storage. The practical example of an employee database demonstrates how nesting structures and file I/O can be used together to manage and store data efficiently. Experiment with different scenarios, explore additional functionalities, and apply these concepts to your programming projects. Mastering nesting structures and file I/O in C will open up a wide range of possibilities for building robust and scalable applications.

Leave a Reply