Please help with this C code on files.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Alwyn
    New Member
    • Mar 2011
    • 1

    Please help with this C code on files.

    Code:
    struct lib
    {
    char *name,*author,*publish;
    int price;
    int no;
    }u[50];
    
    FILE *fp;
    
    void fin()  //Inputs data to a file
    {  int i;
    fp=fopen("abc.txt","w");
    
    for(i=1;i<=n;i++)
    {
    
    fprintf(fp,"%s",&u[i].name);
    
    putc('\t',fp);
    fprintf(fp,"%s",&u[i].author);
    
    putc('\t',fp);
    fprintf(fp,"%s",&u[i].publish);
    
    putc('\t',fp);
    fprintf(fp,"%d",u[i].price);
    putc('\t',fp);
    fprintf(fp,"%d",u[i].no);
    putc('\n',fp);
    
    }
    
    fclose(fp);
    
    }
    
    
    
    void fout() // Reads data from the file 
    {
    int i,x,j=0;
    char c,d[50];
    d[0]='\0';
    fp=fopen("abc.txt","r");
    for(i=1;i<=n;i++)
    {
    fscanf(fp,"%s",u[i].name);
    while((c=getc(fp))!='\t') ;
    
    fscanf(fp,"%s",u[i].author);
    while((c=getc(fp))!='\t') ;
    
    fscanf(fp,"%s",u[i].publish);
    while((c=getc(fp))!='\t') ;
    
    fscanf(fp,"%d",&u[i].price);
    while((c=getc(fp))!='\t') ;
    
    fscanf(fp,"%d",&u[i].no);
    while((c=getc(fp))!='\n') ;
    
    }
    fclose(fp);
    
    }


    The strings read from the file are not stored in the structure. If I try to display the strings they appear as <null> . The numbers are input without any problem.
    Please help as soon as possible.
    Last edited by Dormilich; Mar 21 '11, 10:17 AM. Reason: please use [CODE] [/CODE] tags when posting code
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    the following are pointers
    Code:
    char *name,*author,*publish;
    but what do they point too?
    should they be arrays?
    Code:
    char name[20],author[20],publish[20];

    Comment

    Working...