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,708
    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:
    #include <cmath>
    #include <complex>
    bool euler_flip(bool value)
    {
        return std::pow
        (
            std::complex<float>(std::exp(1.0)), 
            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, 11: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-22-2008, 12:19 AM
  4. help with text input
    By Alphawaves in forum C Programming
    Replies: 8
    Last Post: 04-08-2007, 05:54 PM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM

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