Search
 
SCRIPT & CODE EXAMPLE
 

TYPESCRIPT

read/write linked lists to file

#include <stdio.h>
#include <stdlib.h>

typedef struct Node {
    char name[50];
    int age;
    struct Node *next;
}Node;

// user-defined functions
void printPetRecord(Node *head);
void writeToFile(FILE *fptr, Node *head);

// main()
int main(void)
{
    int count, i;
    Node *petRecord, *newRecord;
    FILE *fp;

    if( (petRecord = malloc(sizeof(Node))) == NULL )
    {
        fprintf(stderr, "Unable to allocate memory.
");
        exit(2);
    }
    newRecord = petRecord;
    printf("How many pets do you have? ");
    scanf("%d", &count);

    for(i = 0; i < count; i++)
    {
        printf("Name of Pet: ");
        scanf("%50s", newRecord->name);
        printf("Age of Pet: ");
        scanf("%d", &newRecord->age);
        if(i == count-1)
        {
            newRecord->next = NULL;
        }
        else
        {
            if( (newRecord->next = malloc(sizeof(Node))) == NULL)
            {
                fprintf(stderr, "Memory Unavailable.
");
                exit(3);
            }
        }
        newRecord = newRecord->next;
    }
    printf("

");
    // Modified arguments
    printPetRecord(petRecord);

    // Open file before sending to writeToFile
    if(!(fp = fopen("petname.txt", "w")))
    {
        fprintf(stderr, "Unable to open file "petname.txt"
");
        exit(1);
    }
    // Modified arguments
    writeToFile(fp, petRecord);
    
    fclose(fp);
    return 0;
}

// function to print linked_list
void printPetRecord(Node *head)
{
    if(head->next != NULL)
    {
        printf("Name of Pet: %s
Age of Pet: %d
", head->name, head->age);
        printPetRecord(head->next);
    }
    else
        printf("Name of Pet: %s
Age of Pet: %d
", head->name, head->age);
}

// function to print list to file
void writeToFile(FILE *fptr, Node *head)
{
    if(head->next != NULL)
    {
        fprintf(fptr, "
Pet Name: %s
Age: %d

", head->name, head->age);
        writeToFile(fptr, head->next);
    }
    else
        fprintf(fptr, "
Pet Name: %s
Age: %d

", head->name, head->age);
}

Comment

PREVIOUS NEXT
Code Example
Typescript :: literal types typescript 
Typescript :: Start Angular App In Localhost 
Typescript :: typescript string 
Typescript :: Unshift type Typescript 
Typescript :: jest not toBe 
Typescript :: Interface with custom property name type 
Typescript :: typescript syntax 
Typescript :: run an applescript 
Typescript :: indexof typescript 
Typescript :: what is hello world in typescript 
Typescript :: typescript deep partial 
Typescript :: how to pring events in pygame 
Typescript :: laravel middleware for apis 
Typescript :: split in angular 8 
Typescript :: firebase typescript 
Typescript :: typescript document.getelementbyid object is possibly null 
Typescript :: Which coutnry doesnt have taxes 
Typescript :: using method parameters in a guard nestjs 
Typescript :: Custom Error Message Class 
Typescript :: what is use hsts in .net core 
Typescript :: highcharts print 
Typescript :: phaser load progress 
Typescript :: google sheets script save A RANGE to csv 
Typescript :: sum the digits in c 
Typescript :: how to loop through a specific number of elements in a list python 
Typescript :: how to use the pokeapi with javascript 
Typescript :: cool_beasts = {"octopuses":"tentacles", "dolphins":"fins", "rhinos":"horns"} for ___ in cool_beasts.items(): print("{} have {}".format(___)) 
Typescript :: hhow to remove elements from java 
Typescript :: isolate digits in large number cpp 
Typescript :: how to check weather a file exists using os module 
ADD CONTENT
Topic
Content
Source link
Name
5+6 =