How to Write a function to store the data of your linked list in a file?
-
This is my linked list..Please somebody help me to make a file of this program.. #include <stdlib.h> #include <stdio.h> #include <string.h> int serial=1; //initializing a global variable void insert_newstudent (); //Function to add new node to linked list void print_database(); //Function to print all nodes of the linked list struct student{ //Node of the linked list char name[100]; int age; float gpa; int serial_number; student *next_ptr; }; //Global pointers to traverse the linked list struct student *first_element,*current_element,*new_ele… int main() { char ch; first_element = NULL; while (1) { printf("\nEnter 'e' to enter new agent\n"); printf("\n 'l' to display all student data\n"); printf("\n'q' to quit the program\n"); ch = getchar(); switch(ch) { case 'e': insert_newstudent(); break; case 'l': print_database(); break; case 'q': return 0; default: puts("\nEnter only selections listed"); } } return 0; } void insert_newstudent () { //Create a new node(sturcture in memory) and point to it new_element = (struct student*) malloc(sizeof(struct student)); if(first_element==NULL) { //If list is empty first_element = current_element = new_element; //All three pointers are pointing at the first location } else { current_element = first_element; //Point to the first node while(current_element->next_ptr !=NULL) //Find the last node so that new node can be added after this { current_element = current_element->next_ptr; } current_element->next_ptr = new_element; //Now that we have found the last node, make its next_ptr point to the new element current_element = new_element; //Now point to this new node in memory so that data can be added to it } _flushall(); printf("\nEnter name: "); gets(current_element->name); printf("\nEnter age: "); scanf("%d",¤t_element->age); printf("\nEnter gpa: "); scanf("%f",¤t_element->gpa); current_element->serial_number=serial… serial++; current_element->next_ptr = NULL; } void print_database() { if(first_element==NULL){ //Can't print anything if list is empty printf("\nEmpty List. \n"); return; } current_element = first_element; //Start from the first element, traverse through the list and print all do { printf("\nName: %s",current_element->name); printf("\nAge: %d",current_element->age); printf("\ngpa: %f\n",current_element->gpa); printf("\nserialnumber:%d\n",current… //printimg the serial number current_element=current_element->nex… } while(current_element != NULL); }
-
Answer:
void writelist(struct student *H) { FILE *op=fopen("filename", "w"); while(H) { fwrite(H, sizeof(struct student), 1, op); H=H->next_ptr; } }
Bakhtawa... at Yahoo! Answers Visit the source
Related Q & A:
- How to add a new table to a data source?Best solution by technet.microsoft.com
- How to call a function with parameter in a bash script?Best solution by tldp.org
- How to fire a function before and after automatically in a jquery plugin?Best solution by catchmyfame.com
- how to call a function in Python in another function?Best solution by Yahoo! Answers
- How to call a function asynchronously in PHP?Best solution by Stack Overflow
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.