Adding new records to a file using a pointer

This is a discussion on Adding new records to a file using a pointer within the C Programming forums, part of the General Programming Boards category; My program is to get 5 fields, including one called 'position' that shows the sequential position of the record in ...

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    2

    Adding new records to a file using a pointer

    My program is to get 5 fields, including one called 'position' that shows the sequential position of the record in the file. If I have 5 records in the file, *.txt, the last position in that file should be 4. If i want to put a new record in it should be position 5. After I use f_open to open the file, I am not sure whether to use f_seek or f_read or both. How can I tell the pointer to point to the last record in the file to get the value of the last position? I want to use that plus 1 to put in the new data.
    thank you.

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Posts
    5,439
    After you close a file, the last file position is lost. Therefore, you must reset it each time for appending...


    FILE *f = fopen(file, "rb+");
    fseek(file, 0, SEEK_END);
    fwrite(&data, sizeof(someStruct), 1, f);
    fclose(f);





    FILE *f = fopen(file, "rb");
    while(fread(&data, sizeof(someStruct), 1, f))
    count++;
    fclose(f);
    Code:
    int main(void){srand(time(0));for(double l=rand(),l0=0,l00=0;;l0+=0.1){for(double l000=0;l000
    <1;l000+=.001,l+=((double)rand()/RAND_MAX)/0x64,l00+=((sin(l*0x8*atan(l0)*l000-(l0*0x8*atan
    (l)))*0.5)+0.5)){l00-=floor(l00);for(size_t l0000=0,l00000=(size_t)(0x50*(l00));l0000<l00000;++l0000
    )putchar(0x20);putchar(0x61+(int)((double)rand()/RAND_MAX*0x1a));putchar('\n');}}return 0;}

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. opening empty file causes access violation
    By trevordunstan in forum C Programming
    Replies: 10
    Last Post: 10-21-2008, 11:19 PM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 04:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 01:46 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21