"read/write linked lists to file" Code Answer's
You're definitely familiar with the best coding language TypeScript that developers use to develop their projects and they get all their queries like "read/write linked lists to file" answered properly. Developers are finding an appropriate answer about read/write linked lists to file related to the TypeScript coding language. By visiting this online portal developers get answers concerning TypeScript codes question like read/write linked lists to file. Enter your desired code related query in the search bar and get every piece of information about TypeScript code related question on read/write linked lists to file.
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.\n");
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.\n");
exit(3);
}
}
newRecord = newRecord->next;
}
printf("\n\n");
// Modified arguments
printPetRecord(petRecord);
// Open file before sending to writeToFile
if(!(fp = fopen("petname.txt", "w")))
{
fprintf(stderr, "Unable to open file \"petname.txt\"\n");
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\nAge of Pet: %d\n", head->name, head->age);
printPetRecord(head->next);
}
else
printf("Name of Pet: %s\nAge of Pet: %d\n", head->name, head->age);
}
// function to print list to file
void writeToFile(FILE *fptr, Node *head)
{
if(head->next != NULL)
{
fprintf(fptr, "\nPet Name: %s\nAge: %d\n\n", head->name, head->age);
writeToFile(fptr, head->next);
}
else
fprintf(fptr, "\nPet Name: %s\nAge: %d\n\n", head->name, head->age);
}
Source: stackoverflow.com
All those coders who are working on the TypeScript based application and are stuck on read/write linked lists to file can get a collection of related answers to their query. Programmers need to enter their query on read/write linked lists to file related to TypeScript code and they'll get their ambiguities clear immediately. On our webpage, there are tutorials about read/write linked lists to file for the programmers working on TypeScript code while coding their module. Coders are also allowed to rectify already present answers of read/write linked lists to file while working on the TypeScript language code. Developers can add up suggestions if they deem fit any other answer relating to "read/write linked lists to file". Visit this developer's friendly online web community, CodeProZone, and get your queries like read/write linked lists to file resolved professionally and stay updated to the latest TypeScript updates.