I am making a program using dynamic memory and was able to successfully code it going into the file, but need help reading it back, I am stuck in the function Load.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ctype.h>
struct point
{
int x; //point x
int y; //point y
char label[21]; //label of 2 points
struct point *ptrNext; // self referential pointer
};
//Global:
struct point *ptrFirst = NULL;//empty list (ptrFirst is the head of the list
struct point *ptrLast = NULL;//empty list (ptrLast is the tail of the list)
//Prototypes:
struct point *CreatePoint();
int isEmptyList(struct point *ptrF);
void PrintList(struct point *ptr);
void ResetList();
struct point *AddToBeginning(struct point *ptrNew);
struct point *AddToEnd(struct point *ptrNew);
struct point* InputRecord(struct point *ptrNew); // used by Add to interactively get the values from the user
void Save(const char* filename);
void Load(const char* filename);
int count=0;
int main()
{
int run=1;
int input=1;
int result=0;
while(run==1)
{
printf(
"1. Add a point at the END of the list.\n"
"2. Add a point at the BEGINNING of the list.\n"
"3. Is the list empty?\n"
"4. Erase all points from the list (reset).\n"
"5. Display the list.\n"
"6. Save the list to a sequential file (reset/replace file contents)\n"
"7. Read the list back from a sequential file\n"
"(reset/replace current memory content)\n"
"0. Exit\n\n"
);
scanf("%d", &input);
switch(input)
{
case 0:
{
ResetList(ptrFirst);
run = 0;
break;
}
case 1:
{
AddToEnd(InputRecord(CreatePoint()));
break;
}
case 2:
{
AddToBeginning(InputRecord(CreatePoint()));
break;
}
case 3:
{
if(isEmptyList(ptrFirst)==0)
printf("The list is empty\n\n");
else
printf("The list is not empty\n\n");
break;
}
case 4:
{
ResetList(ptrFirst);
break;
}
case 5:
{
PrintList(ptrFirst);
break;
}
case 6:
{
result=Save("data.dat");
break;
}
case 7:
{
result=Load("data.dat");
break;
}
default:
{
printf("Invalid entry, try again\n\n");
break;
}
}
}
}
//CreatePoint creates using dynamic memory allocation a new record of type point
//Input: assumes enough dynamic memory is available
//Output: returns a pointer to the enw point, or NULL if not enough memory
struct point *CreatePoint()
{
return (struct point *)(malloc(sizeof(struct point)));
}
//isEmptyList checks if the list is empty
int isEmptyList(struct point *ptrF)
{
if(ptrF==NULL)
return 0;
else
return 1;
}
//PrintList will print out the contents of the list
void PrintList(struct point *ptr)
{
if(ptr!=NULL)
{
printf("x: %d\ny: %d\nlabel: %s\n\n",ptr->x,ptr->y,ptr->label);
PrintList(ptr->ptrNext);
}
}
//ResetList will empty or free() the contents of the list
void ResetList()
{
struct point *ptrDel=ptrFirst;
while(ptrFirst!=NULL)
{
ptrDel=ptrFirst;
ptrFirst=ptrFirst->ptrNext;
free(ptrDel);
}
}
//AddToBeginning adds a new record to the beginning of the list
//Input: assumes a valid record is given and initialized properly
//Output: adds the record to the beginning, returns a pointer to that record
struct point* AddToBeginning(struct point *ptrNew)
{
//case 1: ptrNew is not valid(ie. NULL - nothing to add!)
if(ptrNew==NULL)
return NULL;
//case 2: add the record to an empty list
if(ptrFirst==NULL)
{
ptrFirst=ptrNew;
ptrLast=ptrNew;
}
//case 3: you are adding new record to an existing list
else
{
ptrNew->ptrNext=ptrFirst;
ptrFirst=ptrNew;
}
return ptrNew;
}
//AddToEnd will add a point to the end of the list
struct point *AddToEnd(struct point *ptrNew)
{
//case 1: ptrNew is not valid(ie. NULL - nothing to add!)
if(ptrNew==NULL)
return NULL;
//case 2: add the record to an empty list
if(ptrFirst==NULL)
{
ptrFirst=ptrNew;
ptrLast=ptrNew;
}
//case 3: add the record to an existing list
else
{
ptrLast->ptrNext = ptrNew;
ptrLast = ptrNew;
}
}
//InputRecord sets the initial values of a new record
//Input: a pointer to an existing record
//Output: sets the int value to a value from the keyboard, and null to all pointers
// and returns the same pointer to that initialized record
struct point* InputRecord(struct point *ptrNew)
{
if(ptrNew==NULL) //fail-safe: check for valid pointer
return NULL;
printf("Please enter the data for x and y:\n");
scanf("%d%d", &ptrNew->x,&ptrNew->y);
printf("Please enter the label for the data:\n");
scanf("%s", ptrNew->label);
ptrNew->ptrNext=NULL;
return ptrNew;
}
int Save(const char* filename)
{
count=0;
FILE* outfile;
struct point *ptrCurrent = ptrFirst;
if(isEmptyList(ptrFirst)==0)
return 0;
outfile = fopen(filename, "w");
if (outfile == NULL)
return 0;
while(ptrCurrent != NULL)
{
count++;
fprintf(outfile, "%d %d %s",ptrCurrent->x,ptrCurrent->y,ptrCurrent->label);
if(ptrCurrent->ptrNext != NULL)
fprintf(outfile, "\n");
ptrCurrent=ptrCurrent->ptrNext;
}
fclose(outfile);
}
int Load(const char* filename)
{
FILE* infile;
struct point *ptrNew = NULL;
infile = fopen(filename, "r");
if (infile == NULL)
return 0;
// ready to read from file, reset (clear) current list!
ResetList(ptrFirst);
while(!feof(infile))
{
ptrNew = (struct point *) (malloc(sizeof(struct point)));
if (ptrNew == NULL)
break;
//STUCK HERE
}
Just wondering how you would read it back in using no arrays, any ideas? For now I will assume the words input have no spaces for convenience.