At 1/24/2007 01:08 PM, you wrote:
>Hey, I have a problem in linked list. I made a little database that
>stores name and secret number. But the problem is that when I want to
>save these data in any file so this data saves but real problem is to
>read data from a file. I had posted earlier this problem on this
>group.Hope I have reached to my answer. Plz solve my problems. Here is
>my code

Well, for one, you never initialize ptrthis. But why do that if all 
you want to to do is print the values? Here's a quick and dirty 
re-work of rfile:

void rfile(){

      biodata TEMP;

         FILE *fptr;                     /*file ponter*/
         if( (fptr=fopen(MyFile,"rb")) == NULL) /*if returning NULL*/
           printf("Can't open file %s", MyFile);


         while(fread(&TEMP, sizeof(biodata), 1, fptr) == 1){ /*item 
return or found*/
                  printf("\nName: %s", TEMP.name);  /*print contents*/
                  printf("\nNumber: %03d\n", TEMP.num);
         }
         fclose(fptr);
}

~Rick

>#include <stdio.h>
>#include <conio.h>
>#include <stdlib.h>
>
>#define TRUE 1
>
>void newname();
>void listall();
>void rfile();
>
>struct biodata{
>         char name[30];
>         int num;
>         biodata *ptrnext;
>};
>biodata *ptrfirst, *ptrthis, *ptrnew;
>
>void main(void){
>
>
>         ptrfirst = (struct biodata *) NULL;
>
>         while(TRUE){
>              printf("\nPress 'e' for new entry.\n");
>                 printf("      'l' for list all.\n");
>                 printf("      'r' for read.\n");
>                 printf("      'q' for quit");
>                 char ch;
>                 ch=getch();
>
>                 switch(ch){
>
>                 case 'e':
>                 newname();      /*Enter new name*/
>                 break;
>
>                 case 'l':
>                 listall();      /*List entire agents*/
>                 break;
>
>                 case 'r':
>                 rfile();        /*Read data from file*/
>                 break;
>
>                 case 'q':
>                 exit(0);
>                 break;
>                 }
>         }
>}
>
>void newname(){
>
>         char numstr[15];
>         ptrnew = (struct biodata *) malloc (sizeof(biodata)); /*Gets memory
>space */
>
>         if(ptrfirst == (struct biodata *)NULL)  /*if none already*/
>       ptrfirst = ptrthis = ptrnew;              /*save address*/
> 
>/*not first item*/
>         else{ 
>                                              /*go to end of list*/
>             ptrthis = 
> ptrfirst; 
> /*Start at begin*/
>                 while(ptrthis->ptrnext != (struct biodata *)NULL ){
>                      ptrthis = ptrthis->ptrnext; /*find next item*/
>                 }
>                 ptrthis->ptrnext = ptrnew;      /*point to new item*/
>                 ptrthis = ptrnew;               /*go to new item*/
>         }
>         FILE *fptr;
>         if( (fptr=fopen("E:/MS Visual Studio/link list/biodata.txt", "a")) ==
>NULL)
>           printf("Can't open File.");
>
>         printf("\nEnter name: ");
>         gets(ptrthis->name);
>         printf("Enter number: ");
>         gets(numstr);
>         ptrthis->num = atoi(numstr);
>         fwrite(ptrthis, sizeof(biodata), 1, fptr);
>         fclose(fptr);
>         ptrthis->ptrnext = (struct biodata *) NULL;
>}
>
>void listall(){
>
>         if(ptrfirst == (struct biodata *) 
> NULL)                                 /*if empty list*/
>           printf("\nList is 
> Empty.");                                                   /*return*/
>
>         ptrthis = ptrfirst;           /*start at first item*/
>         while(ptrthis != (struct biodata *) NULL){ /*print contents*/
>         printf("\nName: %s", ptrthis->name);
>         printf("\nNumber: %03d\n", ptrthis->num);
>         ptrthis = ptrthis->ptrnext;             /*move to next item*/
>         }
>
>}
>
>void rfile(){
>
>         FILE *fptr;                     /*file ponter*/
>         if( (fptr=fopen("E:/MS Visual Studio/link list/biodata.txt","r")) ==
>NULL) /*if returning NULL*/
>           printf("Can't open file");
>
>         while(fread(ptrthis, sizeof(biodata), 1, fptr) == 1){ /*item return
>or found*/
>                 printf("\nName: %s", ptrthis->name);  /*print contents*/
>                 printf("\nNumber: %03d\n", ptrthis->num);
>                 ptrthis = ptrthis->ptrnext;
>         }
>         fclose(fptr);
>}
>
>
>
>
>
>To unsubscribe, send a blank message to 
><mailto:[EMAIL PROTECTED]>.
>Yahoo! Groups Links
>
>
>

Reply via email to