Thread: what's wrong with this code ?

  1. #1
    Registered User
    Join Date
    Sep 2001
    Posts
    9

    what's wrong with this code ?

    i want to edit contents of binary file in which records are stored
    record is a structure.

    ftp = fopen("stock.dat", "a+" );

    switch(ch)
    {
    case 2: // option to edit record
    {
    long int rec = 0, loc = 0;
    printf("\n Enter record no :- ");
    scanf("%ld", &rec);
    loc = ( rec - 1 ) * sizeof(a) + ( rec - 1 );
    fseek( fpt, loc, 0 );
    getdata();
    fread( &a, sizeof(a), 1, fpt );
    break;
    }
    }

    now problem is that file pointer is positioning properly
    but the record is not updated.
    please help.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: what's wrong with this code ?

    Originally posted by abhiboy007
    i want to edit contents of binary file in which records are stored
    record is a structure.

    ftp = fopen("stock.dat", "a+" ); 1

    switch(ch) 2
    {
    case 2: // option to edit record
    {
    long int rec = 0, loc = 0; 3
    printf("\n Enter record no :- ");
    scanf("%ld", &rec); 4
    loc = ( rec - 1 ) * sizeof(a) + ( rec - 1 ); 5
    fseek( fpt, loc, 0 ); 6
    getdata(); 7:
    fread( &a, sizeof(a), 1, fpt );
    break;
    }
    }

    now problem is that file pointer is positioning properly
    but the record is not updated.
    please help.
    1: you don't test the return value
    2: where the hell did ch come from?
    3: While valid, it is 'Bad Form(TM)'.
    4: What is 'rec'?
    5: What is 'a'?
    6: Poor use of fseek.
    7: What the hell is this?

    The rest has already been covered.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    May 2002
    Posts
    3
    I'm just a beginner but I do notice that, in your 1st line:

    ftp = fopen("stock.dat", "a+" ); 1

    shouldn't the ftp be fpt as follows in the remaining code ?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. what is wrong in this simple code
    By vikingcarioca in forum C Programming
    Replies: 4
    Last Post: 04-23-2009, 07:10 AM
  2. what is wrong with this code please
    By korbitz in forum Windows Programming
    Replies: 3
    Last Post: 03-05-2004, 10:11 AM
  3. I cant find what is wrong with this code
    By senegene in forum C Programming
    Replies: 1
    Last Post: 11-12-2002, 06:32 PM
  4. Anyone see what is wrong with this code?
    By Wise1 in forum C Programming
    Replies: 2
    Last Post: 02-13-2002, 02:01 PM
  5. very simple code, please check to see whats wrong
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 10-10-2001, 12:51 AM