Saving files

This is a discussion on Saving files within the C Programming forums, part of the General Programming Boards category; Hi! I have a function which saves properties of the program into the setup.cfg file. There are two properties to ...

  1. #1
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669

    Question Saving files

    Hi!

    I have a function which saves properties of the program into the setup.cfg file. There are two properties to be saved. My problem is that when I want to save just one propertie I have to read the whole setup.cfg file and save the previous properties into the temporary variable. Is there any way to avoid this? If not, is there any other solution?

    Here's my code:
    Code:
    typedef struct
    {
       char LanguageFile[30];
       BOOL StatusLine;
    } Properties;
    
    # define SETUP_FILE "./DATA/setup.cfg"
    
    int main ()
    {
       FILE *fin = NULL;
       Properties File;
       char Temp[30];
    
       if ((fin = fopen (SETUP_FILE, "rb")) != NULL)
       {
          fread (&File, sizeof (Properties), 1, fin);
          strncpy (Temp, File.LanguageFile, strlen (File.LanguageFile) + 1);
          strncpy (File.LanguageFile, Temp, strlen (Temp) + 1);
       }
       fclose (fin);
       if ((fin = fopen (SETUP_FILE, "wb")) != NULL)
       {	
          strncpy (File.LanguageFile, Temp, strlen (Temp) + 1);	
          File.StatusLine = TRUE;
          fwrite (&File, sizeof (Properties), 1, fin);
       }
       fclose (fin);
    }
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    Instead of using the "wb" file open mode, which erases the file in question, use "ab", which preserves the previous contents and places the file pointer at the end of the file for writing.

    -Prelude
    My best code is written with the delete key.

  3. #3
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    But this will always add the saved propertie into a file. At the end I'll have a 1 KB of saved properties which is not good.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    I see, so you want to have a set number of properties in the .cfg file and update the file when one property changes?

    If the size of each property doesn't change then you could easily index the file with a table of offsets placed at the beginning of the file. When you want to update a property, read the table and go to the appropriate offset to make the change.

    If the file isn't huge and the size of properties will change, your approach is probably the best for small files. Anything more will overcomplicate things if the speed gain isn't worth the extra code. This is usually true for the smaller files.

    -Prelude
    My best code is written with the delete key.

  5. #5
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I see, so you want to have a set number of properties in the .cfg file and update the file when one property changes?
    Yes.

    If the size of each property doesn't change then you could easily index the file with a table of offsets placed at the beginning of the file. When you want to update a property, read the table and go to the appropriate offset to make the change.
    How to index the file?

    If the file isn't huge and the size of properties will change, your approach is probably the best for small files. Anything more will overcomplicate things if the speed gain isn't worth the extra code. This is usually true for the smaller files.
    The file size is very small, it' size will be around 0.5 KB.
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >The file size is very small, it' size will be around 0.5 KB.
    There's little need for efficiency then.

    >How to index the file?
    Each property has a size, create the indexes by breaking the file up into blocks of those sizes and divide the total size of the file by the size of one block. You calculate the offsets by starting at 0 and moving forward with fseek by the size of a block. So if each property has a size of 12 bytes the offset table would be {0, 12, 24, etc...}. To go to a property, just select a key which matches the property with its offset and fseek to that offset.

    -Prelude
    My best code is written with the delete key.

  7. #7
    Code Warrior
    Join Date
    Nov 2001
    Posts
    669
    I was trying with the fseek function but I just can't get it working. Can you show me an example?
    Current projects:
    1) User Interface Development Kit (C++)
    2) HTML SDK (C++)
    3) Classes (C++)
    4) INI Editor (Delphi)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. saving name characters to files
    By clonedhero in forum C Programming
    Replies: 5
    Last Post: 01-16-2008, 03:20 PM
  2. Saving numbers to files
    By cpudaman in forum C++ Programming
    Replies: 5
    Last Post: 01-10-2008, 01:05 AM
  3. New to C++ problem saving files
    By wesm in forum C++ Programming
    Replies: 2
    Last Post: 11-02-2005, 02:00 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 09:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 04:30 PM

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