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
    Location
    Waterloo, Texas
    Posts
    5,659
    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);



    ITSA
    Socket Library!

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