Thread: Adding new records to a file using a pointer

  1. #1
    Registered User
    Join Date
    Apr 2002

    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
    Waterloo, Texas
    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);

    FILE *f = fopen(file, "rb");
    while(fread(&data, sizeof(someStruct), 1, f))
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
        return std::pow
            std::complex<float>(0, 1) 
            * std::complex<float>(std::atan(1.0)
            *(1 << (value + 2)))
        ).real() < 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, 09: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, 02:46 PM