Thread: keeping track of # of files

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    3

    keeping track of # of files



    help! I'm trying out c programming and I'm stuck in this certain part.

    I have an array of structures (max of 10) which i need to store in a non-text file.

    You see, my program goes that there is a registration part. So every time someone signs up, i have to save it in the file using one of the array of structure.

    So how do I keep track of how many I've already stored in the file, and how do I know at what position do I save it? I tried it out using loops but it doesn't seem to work. Am I doing it right like this: ??

    if ((pUser = fopen("buyerstemp.dat", "w+b")) != NULL)
    {
    while(!feof(pUser))
    {
    fread (&aUser[i], sizeof(arrUser), 1, pUser);
    if (!feof(pUser))
    i++;
    }

    //**the rest here is the part wherein I'm asking the necessary info from the user **//
    then at the end part I have the fwrite thing.

    Am I doing this right? Help!

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well if the file is small (as seems to be the case), you should just read the whole file into the array in one go with
      fread( aUser, sizeof(arrUser), 10, pUser);

    And write it back out again using
      fseek( pUser, 0, SEEK_SET );
      fwrite( aUser, sizeof(arrUser), 10, pUser);

    > So how do I keep track of how many I've already stored in the file
    Presumably some field in your structure marks it as being either in-use for free.
    An initial count when you've read the array in will keep you up to date.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Create Copies of Files
    By Kanshu in forum C++ Programming
    Replies: 13
    Last Post: 05-09-2009, 07:53 AM
  2. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  3. *.cpp and *.h files understanding
    By ElastoManiac in forum C++ Programming
    Replies: 4
    Last Post: 06-11-2006, 04:45 AM
  4. Linking header files, Source files and main program(Accel. C++)
    By Daniel Primed in forum C++ Programming
    Replies: 3
    Last Post: 01-17-2006, 11:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM