keeping track of # of files

This is a discussion on keeping track of # of files within the C Programming forums, part of the General Programming Boards category; help! I'm trying out c programming and I'm stuck in this certain part. I have an array of structures (max ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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, 10:46 AM
  5. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 01:41 PM

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