Thread: different sized data structures

  1. #1
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91

    different sized data structures

    Hi all!
    youve probably seen this type of Q before, but i'm pulling my hair out with what might seem very a very trivial issue.

    I have three data structures>>

    struct issue_return_rec{
    char part_num[7],
    quantity[5];
    };

    struct create_rec{
    char cust_name[21],
    cust_addr[61],
    cust_bal[10],
    credit_limit[8];
    };

    union details{
    struct issue_return_rec i_r;
    struct create_rec crt;
    };

    typedef struct{
    char rec_type;
    char cust_code[6];
    union details cust_rec;
    }RECORDS;

    I also have a file that holds varied sized records, a delete rec is 17 cahrs long a issue rec is 19 chars long and a create record that is 107 chars long.
    The file has to be opened in binary format, then allocated memory, then its to be read from the allocated memory sorted then written to a newly sorted file.

    The next part of my code is where i have the problem.......bare with me on this.

    int main()
    {
    RECORDS trans; /*ident of struct*/
    RECORDS *trns_ptr;

    unsigned long int recs_num=0, file_size=0;

    FILE *sort_fp, *valid_fp;

    valid_fp = open_file("A:GJ23VF.DAT", "rb");
    sort_fp = open_file("A:GJ23SD.DAT", "wb+");


    fseek(valid_fp, 0L, SEEK_END);

    file_size = ftell(valid_fp);

    recs_num = file_size / sizeof(RECORDS);
    /-*here's the problem*/
    }
    I know that the file that is being read has 160 records on it, ftell() returns 3040 bytes, now because the data in the file that i need to manipulate are of different sizes. recs_num will only hold 28 records, this is because of the sizeof(RECORDS).
    So when i try to sort the records it only sorts 28 records

    Is there another way to solve my problem of getting recs_num to hold the value of the correct amount of records.

    thanks in adv
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Cool

    If you have a file containing various record sizes then each record on the file should have a 'Record Type Field'
    i.e. a record might look like this:
    Record Type - 'D'
    Customer Ref - '12345'

    Rather than using :
    fseek.....
    ftell.....
    recs_num = file_size / sizeof(RECORDS);

    Try reading in the first character of the file (which should be the record type of the first record). Depending on the record type read in the correct number of bytes (i.e. Deletion 17 characters, Issue 19 characters and create 107 characters into the allocated memory space. The file pointer will now be pointing to the record type of the second record so this record type can now be identified and read into memory and so on...

    Example:
    char temp_store; (/*a single char to store initial rec type*/

    f_ptr=fopen("yourfile","rb");


    while(f_ptr)
    {
    fread(temp_store,sizeof(char),1,f_ptr);

    /*now move file pointer back to start of record*/
    fseek(f_ptr,(long)-1*sizeof(char),SEKK_CUR);

    switch(temp_store)
    {
    case 'C':fread(array_ptr,sizeof(struct create),1,f_ptr);
    :break;
    case 'D':fread(......................
    case 'I' :fread(......................
    }
    }/*end while*/

    fclose(f_ptr) /*re-open after sorting records so data can be written back from allocated memory*/

    Hope this helps
    (I think it should work)

  3. #3
    Registered User
    Join Date
    Aug 2001
    Posts
    247
    Hi brother breed,
    program 2 now, way to go.

    try using long for your variables file_size and rec_num instead of unsigned long. Let me know how you get on. All four completed and posted (e-mailed here).
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  4. #4
    Registered User breed's Avatar
    Join Date
    Oct 2001
    Posts
    91
    Thanks dan the man, i'll try your suggestion out, although i might have to restructure my prog to do it, though if it works then it'll be worth th effort e'h

    Hey Big tam how's things, haven't you got your results yet?
    have them lazy b******ds forgot about you

    I hope you pass with colours mate!, keep in touch guy, you got me mail add.

    I have got to go in this week 07/03/02 (that's for definate this time , cos i made sure i didn't forget to reply )

    taken a week off work to crash it, i struggled last time cos i let it all slip too far

    Good luck Tam(not that you'll need it)

    &Thanks again Dan the man
    Before you judge a man, walk a mile in his
    shoes. After that, who cares.. He's a mile away and you've got
    his shoes.
    ************William Connoly

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. What's a good generalized data structures book?
    By indigo0086 in forum A Brief History of Cprogramming.com
    Replies: 12
    Last Post: 11-16-2006, 01:01 PM
  2. i need advice about data structures
    By sawer in forum C Programming
    Replies: 2
    Last Post: 04-22-2006, 03:40 AM
  3. Need some help regarding data structures
    By Afrinux in forum C Programming
    Replies: 15
    Last Post: 01-28-2006, 05:19 AM
  4. C Programming Question
    By TK in forum A Brief History of Cprogramming.com
    Replies: 13
    Last Post: 07-04-2002, 07:11 PM
  5. Array Data Structures
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 03-27-2002, 06:52 PM