Monday, April 28, 2025

C Tutorial - 28 - Write a Structure to a Text File with C

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

struct Books {
    char Title[50];
    char Author[50];
    char Topic[100];
    int id_book;
};

int main() {

    struct Books Book_1;

    FILE *fp = fopen("output_file.txt", "w");

    if (fp == NULL) {
        printf("Unable to open file for writing. \n");
    }
    else {

        strcpy(Book_1.Title, "Relativity T");
        strcpy(Book_1.Author, "Albert E");
        strcpy(Book_1.Topic, "Relativity");
        Book_1.id_book = 4323;

        fprintf(fp,"Book_1 - Title: %s \n", Book_1.Title);
        fprintf(fp,"Book_1 - Author: %s \n", Book_1.Author);
        fprintf(fp,"Book_1 - Topic: %s \n", Book_1.Topic);
        fprintf(fp,"Book_1 - ID: %d \n", Book_1.id_book);

        fclose(fp);

        printf("-------------------------- \n");
        printf("Data Writen to a File.     \n");
        printf("File Closed.     \n");
        printf("-------------------------- \n");
    }

    return 0;
}

Result:

--------------------------
Data Writen to a File.
File Closed.
--------------------------

Process returned 0 (0x0)   execution time : 0.016 s
Press any key to continue.

This is a C program that defines a struct named "Books" to store information about books, opens a file named "output_file.txt" for writing, writes data about a book to the file using fprintf(), and then closes the file using fclose(). Here is a brief explanation of how the program works:

  1. The program starts by defining a struct named "Books" that has four members: Title, Author, Topic, and id_book.

  2. Then, it declares an instance of the "Books" struct named "Book_1".

  3. The program declares a pointer to a file named "fp" and uses the fopen() function to open the file "output_file.txt" in write mode.

  4. If the file is not opened successfully (i.e., if fp is NULL), the program prints an error message.

  5. If the file is opened successfully, the program uses the strcpy() function to set the values of Book_1's Title, Author, Topic, and id_book members.

  6. The program then writes the values of Book_1's members to the file using the fprintf() function.

  7. After writing the data to the file, the program closes the file using fclose().

  8. Finally, the program prints a message indicating that the data has been written to the file and the file has been closed.

Note that this program assumes that the file "output_file.txt" can be created and written to. If the file cannot be created or written to, the program will print an error message and exit without writing any data to the file. Additionally, this program writes data for only one book, but it can be modified to write data for multiple books by using arrays or linked lists of "Books" structs.

No comments:

Post a Comment

Tkinter Introduction - Top Widget, Method, Button

First, let's make shure that our tkinter module is working ok with simple  for loop that will spawn 5 instances of blank Tk window .  ...