Thread: Index files

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    3

    Index files

    Still have a problem with locating a record in a dynamic array,
    overwriting the field with a 'Q' and writing whole array to a file.

    On examining the file a 'Q' is written to it but it writes it at the
    beginning of the file not in the position denoted by the rec_num.

    I've rewritten this part.

    Anyone any ideas?


    Also the search does find the correct record and where it is in the
    master file.

    Here's the code snippet:

    static RECORD_DATA *search_file(char *key)
    {
    static RECORD_DATA record;
    static RECORD_IDX *idx_list,
    *entry_ptr;

    register long recs;



    FILE *fp_idx;


    fp_idx=fopen("A:\STCKMAST.IDX","rb+");


    fseek(fp_idx,IDXSIZE,SEEK_END);
    recs = (ftell(fp_idx)/IDXSIZE);

    /* use malloc(), create alist */
    idx_list = (RECORD_IDX *)malloc(recs * IDXSIZE);
    /*
    {
    printf("\nInsufficient memory available. This program will
    close.");
    pause();
    exit(1);
    } */

    rewind( fp_idx);
    fread(idx_list, IDXSIZE * recs,1,fp_idx);
    rewind (fp_idx);

    /* index = *entry_ptr; */
    /* strcpy(index.key, key); */

    qsort(idx_list, recs, IDXSIZE, compare);

    strcpy(index.key, key);

    entry_ptr = (RECORD_IDX *) bsearch(&index, idx_list, recs,
    IDXSIZE, compare);




    /* new bit */
    if(entry_ptr)
    {
    switch(master.ARec.rec_type)
    {
    case 'z':
    case 'Z': /* delete part num from index file */
    index = *entry_ptr;
    /* this bit causes Q to be written at the beginning of the file not
    in the space found during the search */

    strcpy(index.key, "Q");
    printf("\n rec num %ld ",entry_ptr->rec_num);

    /* entry_ptr->key[0]='Q';*/
    /* write whole dynamic array to file */
    fwrite(idx_list, (IDXSIZE * recs),1,fp_idx);
    pause();
    }

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    The problem is with the way you're opening your file. You really should read up on fopen.
    r+ Open for reading and writing. The stream is posi-
    tioned at the beginning of the file.
    You will want to use a+ instead of r+. This will set the file in append mode.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Thanks for the info, I'm not looking to append the file.

    What I'm trying to do is to sort the file once its in the dynamic array and then amend it and then write the array as the new file, so I thought it would just overwrite it. (I could be wrong on this..)

    If I append the array to the file then the file will contain similar index info twice..or am I missing something?

    Thanks

    Andy.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    In that case, just read the entire file, and then write it over top of the one that's there.

    open the file for reading
    read the whole file
    close the file
    open the file for writing (don't use the +)
    write the whole file
    close the file

    Otherwise, open it as you originally did, read the data, call "rewind()" on the file after you've read it all, and then write the whole thing.

    I would still advice reading over the link I provided to see what fopen method would work best for your example.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Feb 2003
    Posts
    3
    Thanks for the reply,

    am working on it.

    Cheers..

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Error opening files in a different dir
    By Ozzie in forum C++ Programming
    Replies: 3
    Last Post: 10-09-2008, 06:55 AM
  2. 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
  3. Multiple Cpp Files
    By w4ck0z in forum C++ Programming
    Replies: 5
    Last Post: 11-14-2005, 02:41 PM
  4. Folding@Home Cboard team?
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 398
    Last Post: 10-11-2005, 08:44 AM
  5. Batch file programming
    By year2038bug in forum Tech Board
    Replies: 10
    Last Post: 09-05-2005, 03:30 PM