how do i open a file and use its content instead of asking the use?
in c language/
there are a few more problems i am having.
I also need help with putting the info in a linklist situation. the
file is a txt of information of ppl. i need to scroll the txt with the
current person's info showing, on the menu. options avaible on the menu:
edit
next person
previous person
delete
Current info:BLah di Blah
Do i need multiply arrays for the different info?
how would i use malloc to allocate 100 contacts?
If i allocated 100 contacts, won't i be wasting room if i had two, how
to fix this?
TRY IT...IT KINDA WORKS!
/* Linked list example */
#include <stdio.h>
#include <string.h>
#include "LinkedList.h"
Node *gptTop; /* Top node of the list */
int main ()
{
Node *ptCur = NULL; /* current node */
Node *pt = NULL; /* temp node */
int bDone = 0; /* notes when user wants to quit */
char ac[128]; /* temp storage for text entry */
int i;
char lastname[80];
char firstname[80];
char email1 [120];
char email2 [120];
FILE *file_ptr;
file_ptr = fopen("emailbook.txt", "w+");
gptTop = NULL;
while (!bDone)
{
/* Display menu */
// Clear screen first
for (i=0; i<30; i++) printf("\n");
printf (" Main Menu\n");
printf ("--------------\n");
printf ("1) Add Contact\n");
printf ("2) Delete Current Contact\n");
printf ("3) Prev Contact\n");
printf ("4) Next Contact\n");
printf ("5) Move Contact\n"); /*need to change to print all
contact*/
printf ("6) Quit\n");
printf ("\n");
/* Display current node's text and whole list */
if (ptCur)
printf ("Current = %s \n", lastname);
/*pt = gptTop;
while (pt)
{
printf (" %s", pt->acText);
pt = pt->ptNext;
}
printf ("\n");*/
/* Handle user input */
printf( "\n\n\nEnter your choice:");
switch (atoi (gets (ac)))
{
case 1:
printf ("Last Name of Contact:");
gets (lastname);
printf ("First Name of Contact:");
gets (lastname);
printf ("Email Address #1:");
gets (lastname);
printf ("Email Address #2:");
gets (lastname);
ptCur = InsertNode (ptCur, gets (lastname));
break;
case 2:
ptCur = DelNode (ptCur);
break;
case 3:
if (ptCur)
if (ptCur->ptPrev)
ptCur = ptCur->ptPrev;
break;
case 4:
if (ptCur)
if (ptCur->ptNext)
ptCur = ptCur->ptNext;
break;
case 5:
printf ("After node with what text (Enter '~' for top
of list)?");
gets (ac);
if (ac[0] != '~')
{
pt = FindNode (gptTop, ac);
if (pt)
MoveNode (ptCur, pt);
}
else
{
MoveNode (ptCur, NULL);
}
break;
case 6:
bDone = 1;
break;
} /* switch(atoi(gets(ac))) */
} /* while (!bDone) */
}
Node * CreateNode (char *ac)
{
Node * ptNewNode;
if ((ptNewNode = (Node *) malloc (sizeof (Node))) == NULL)
return NULL;
strcpy (ptNewNode->acText, ac);
ptNewNode->ptPrev = NULL;
ptNewNode->ptNext = NULL;
return ptNewNode;
} /* CreateNode */
Node * DelNode (Node * pt) {
Node * ptReturn;
if (pt == NULL)
return NULL; /* safety check */
if (pt == gptTop) /* delete top node */
{
if (pt->ptNext) /* not the only node in list */
{
ptReturn = pt->ptNext;
gptTop = ptReturn;
ptReturn->ptPrev = NULL;
}
else /* only node in list */
{
ptReturn = NULL;
gptTop = NULL;
}
}
else /* not deleting top node */
{
if (pt->ptNext) /* not the last node in the list */
{
ptReturn = pt->ptNext;
ptReturn->ptPrev = pt->ptPrev;
pt->ptPrev->ptNext = ptReturn;
}
else /* deleting last node in list */
{
ptReturn = pt->ptPrev;
ptReturn->ptNext = NULL;
}
}
free (pt);
return (ptReturn);
} /* DelNode */
/* To insert at top of list set ptPrev to NULL */
Node * InsertNode (Node * ptPrev, char *ac)
{
Node * ptNewEle;
ptNewEle = CreateNode (ac);
if (ptPrev) /* not inserting at top of list */
{
ptNewEle->ptPrev = ptPrev;
ptNewEle->ptNext = ptPrev->ptNext;
ptPrev->ptNext = ptNewEle;
if (ptNewEle->ptNext) /* Verify there is a next nodeĀ
*/
ptNewEle->ptNext->ptPrev = ptNewEle;
}
else /* insert node at top of list */
{
ptNewEle->ptPrev = NULL; /* for clarity */
ptNewEle->ptNext = gptTop;
if (gptTop)
gptTop->ptPrev = ptNewEle;
gptTop = ptNewEle;
}
return (ptNewEle);
} /* InsertNode */
/* To move a node to the start of the list, set ptPrevAfter to NULL */
void MoveNode (Node * pt, Node * ptPrevAfter)
{
/* safety checks */
if (pt == NULL)
return; /* No node to move */
if (pt == ptPrevAfter)
return; /* Trying to move nowhere */
if (pt == gptTop)
{
if (pt->ptNext == NULL)
return; /* Only one node in list */
if (ptPrevAfter == NULL)
return; /* Trying to move top node to top */
}
if (pt->ptPrev)
pt->ptPrev->ptNext = pt->ptNext; /* Not moving top */
else
gptTop = pt->ptNext; /* Moving top node */
if (pt->ptNext)
pt->ptNext->ptPrev = pt->ptPrev; /* Not moving end node
*/
if (ptPrevAfter) /* not moving node to top */
{
if (ptPrevAfter->ptNext) /* Moving to the end of list ?
*/
ptPrevAfter->ptNext->ptPrev = pt;
pt->ptNext = ptPrevAfter->ptNext;
ptPrevAfter->ptNext = pt;
pt->ptPrev = ptPrevAfter;
}
else /* moving node to top */
{
gptTop->ptPrev = pt;
pt->ptNext = gptTop;
pt->ptPrev = NULL;
gptTop = pt;
}
} /* MoveNode */
Node * FindNode (Node * pt, char *ac)
{
char lastname[120];
/* move through list until either a Node with the same value is found
or the end of the list is reached */
while (pt)
{
if (strstr (pt->acText, lastname))
return (pt);
pt = pt->ptNext;
} /* while (pt) */
return (NULL);
} /* FindNode */