--- "sharique.khan" <[EMAIL PROTECTED]> 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
> 
> #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);
> }
> 
1) I don't see any code where you are writing the
linked list data to a file.

2) When you read the data back in using fread, you
should open the file as "rb".

3) When you read the data back in, you are not
allocating any memory to read it into.  You set
ptrthis to ptrnext but that pointer no longer has any
meaning.

Get a good textbook and read up on fread and fwrite.

Ray


 
____________________________________________________________________________________
The fish are biting. 
Get more visitors on your site using Yahoo! Search Marketing.
http://searchmarketing.yahoo.com/arp/sponsoredsearch_v2.php

Reply via email to