#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2

    File updating problem.


    /*In this prog if the book already existing and again we are entering it, so the counts of the both get added and hence result the new count.
    But that condition is not working.
    while I used GDB I got that, results are added and don't know why this happen.
    Please help me to figure out this!
    */

    typedef struct
    {
    char aname[50],bname[50],price[10],no[10];
    }book;


    void reads(char c[])
    {
    fflush(stdin);
    int i=0;
    while((c[i++]=getchar())!=EOF)
    {
    if(c[i-1]=='\n')
    {
    c[--i]='\0';
    break;
    }
    }
    }


    int add()
    {
    int d;
    char buffer[20];
    printf("\n");
    FILE *p,*q;
    book *b,*r,*temp;
    b=malloc(sizeof(book));
    r=malloc(sizeof(book));
    p=fopen("stock","r");
    q=fopen("stocktemp","w");
    printf("\nEnter book name:");
    reads(b->bname);
    while(!feof(p))
    {
    fread(r,sizeof(book),1,p);
    if (feof(p))
    break;
    if(! (strcmp(b->bname,r->bname) ))
    {
    temp=malloc(sizeof(book));
    printf("\nEnter no:");
    reads(b->no);
    d=atoi(b->no);
    d=d+atoi(r->no);
    //sprintf(buffer,"%d",d);
    strcpy(temp->bname,b->bname);
    strcpy(temp->aname,r->aname);
    strcpy(temp->no,"546");
    strcpy(temp->price,r->price);
    fwrite(temp, sizeof(book), 1, q);
    return 0;
    }
    else
    fwrite(r, sizeof(book), 1, q);
    }
    printf("\nEnter author name:");
    reads(b->aname);
    printf("\nEnter no:");
    reads(b->no);
    printf("\nEnter book price:");
    reads(b->price);
    fwrite(b, sizeof(book), 1, q);
    fclose(p);
    fclose(q);
    rename("stocktemp","stock");
    }
  2. #2
  3. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Unreadable code. Since you couldn't be bothered to use code tags to make it readable, I couldn't be bothered to try to read it. After 34 posts, you do about code tags, so you're just being deliberately rude.

    fflush(stdin) is undefined. Don't use it!

    I trust that you do realize that !strcmp is true if strcmp returns zero. And I trust that you do know what it means when strcmp returns zero. "Know" as in "you have read the documentation instead of relying on your faulty memory and comprehension."
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    35
    Rep Power
    2
    Sorry. I am pretty new to this field. When I searched I got this fflush(stdin).
    I can't able to understand what you are telling.
    Can you please explain more elaborately plz.
  6. #4
  7. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    Originally Posted by aswinas
    Sorry. I am pretty new to this field. When I searched I got this fflush(stdin).
    I can't able to understand what you are telling.
    Can you please explain more elaborately plz.
    USE CODE TAGS! What part of that could you not possibly understand?

    Indent your code properly so that we can read it! Without that, it's just a jumbled mess. HTML strips out that indenting, turning formatted code into an unreadable mess.

    You already know about code tags. You already know why you need to use them. You are willfully refusing to post code that we can read.

    USE CODE TAGS! Then we will be able to try to help you.
  8. #5
  9. Contributing User
    Devshed Supreme Being (6500+ posts)

    Join Date
    Jan 2003
    Location
    USA
    Posts
    7,184
    Rep Power
    2222
    This is what your code looks like with code tags.

    ALWAYS USE CODE TAGS TO POST YOUR CODE! Please explain why you claim to be unable to understand that.

    Code:
    typedef struct
    {
    char aname[50],bname[50],price[10],no[10];
    }book;
    
    
    void reads(char c[])
    {
        fflush(stdin);
        int i=0;
        while((c[i++]=getchar())!=EOF)
        {
            if(c[i-1]=='\n')
            {
                c[--i]='\0';
                break;
            }
        }
    }
    
    
    int add()
    {
    int d;
    char buffer[20];
    printf("\n");
    FILE *p,*q;
    book *b,*r,*temp;
    b=malloc(sizeof(book));
    r=malloc(sizeof(book));
    p=fopen("stock","r");
    q=fopen("stocktemp","w");
    printf("\nEnter book name:");
    reads(b->bname);
    while(!feof(p))
    {
    fread(r,sizeof(book),1,p);
    if (feof(p))
    break;
            if(! (strcmp(b->bname,r->bname) ))
            {
    temp=malloc(sizeof(book));
            printf("\nEnter no:");
            reads(b->no);
            d=atoi(b->no);
            d=d+atoi(r->no);
            //sprintf(buffer,"%d",d);
            strcpy(temp->bname,b->bname);
            strcpy(temp->aname,r->aname);
            strcpy(temp->no,"546");
            strcpy(temp->price,r->price);
            fwrite(temp, sizeof(book), 1, q);
            return 0;
            }
            else
            fwrite(r, sizeof(book), 1, q);
    }
    printf("\nEnter author name:");
            reads(b->aname);
            printf("\nEnter no:");
            reads(b->no);
            printf("\nEnter book price:");
            reads(b->price);
            fwrite(b, sizeof(book), 1, q);
    fclose(p);
    fclose(q);
    rename("stocktemp","stock");
    }
    That is some of the worst formatting I've ever seen! Even with code tags your code is unreadable!

    Format your code properly!

    PS
    And you forgot to #include any header files for the library functions that you are using.
    Last edited by dwise1_aol; November 24th, 2013 at 01:41 PM.

IMN logo majestic logo threadwatch logo seochat tools logo