Complementary task for topic: 11

M Nemeth · 2023-08-29 15:21:04.635218'

Linked lists: File IO

Linked lists: File IO

Create a function to write the names from the linked list to a text file.

Create a text file named "names.txt" with one name per line:
John Doe
Alice Smith
Bob Johnson

Hint: Write the C program to read and write the linked list from/to the file

Solution
#include 
#include 
#include 

// Define the structure for a node in the singly linked list
struct Node {
    char name[100];
    struct Node* next;
};

// Function to create a new node with the given name
struct Node* createNode(const char* name) {
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    strcpy(newNode->name, name);
    newNode->next = NULL;
    return newNode;
}

// Function to add a new node at the end of the linked list
void addToEnd(struct Node** head, const char* name) {
    struct Node* newNode = createNode(name);
    if (*head == NULL) {
        *head = newNode;
    } else {
        struct Node* current = *head;
        while (current->next != NULL) {
            current = current->next;
        }
        current->next = newNode;
    }
}

// Function to print the elements of the linked list
void printList(struct Node* head) {
    struct Node* current = head;
    while (current != NULL) {
        printf("%s\n", current->name);
        current = current->next;
    }
}

// Function to read the linked list from a file
void readLinkedListFromFile(const char* filename, struct Node** head) {
    FILE* file = fopen(filename, "r");
    if (file == NULL) {
        printf("Error opening file: %s\n", filename);
        return;
    }

    char buffer[100];
    while (fgets(buffer, sizeof(buffer), file) != NULL) {
        buffer[strcspn(buffer, "\n")] = '\0'; // Remove newline character
        addToEnd(head, buffer);
    }

    fclose(file);
}

// Function to write the linked list to a file
void writeLinkedListToFile(const char* filename, struct Node* head) {
    FILE* file = fopen(filename, "w");
    if (file == NULL) {
        printf("Error opening file: %s\n", filename);
        return;
    }

    struct Node* current = head;
    while (current != NULL) {
        fprintf(file, "%s\n", current->name);
        current = current->next;
    }

    fclose(file);
}

// Function to destroy the entire linked list and free memory
void destroyList(struct Node** head) {
    struct Node* current = *head;
    struct Node* next;

    while (current != NULL) {
        next = current->next;
        free(current);
        current = next;
    }

    *head = NULL; // Set head to NULL to indicate the list is now empty
}

int main() {
    struct Node* head = NULL;
    const char* filename = "names.txt";

    readLinkedListFromFile(filename, &head);

    printf("Names read from file and stored in the linked list:\n");
    printList(head);

    // Add more names to the linked list (for demonstration purposes)
    addToEnd(&head, "Eve Anderson");
    addToEnd(&head, "David Johnson");

    // Write the updated linked list to the file
    const char* updatedFilename = "updated_names.txt";
    writeLinkedListToFile(updatedFilename, head);
    printf("Names written to file: %s\n", updatedFilename);

    destroyList(&head);

    return 0;
}



Explanation

< < previous    next > >