TIP: Click on subject to list as thread! ANSI
echo: c_plusplus
to: CLIFF RHODES
from: PAUL DRUGGITT
date: 1997-05-29 00:04:00
subject: passing structures to fu

 -=> Quoting Cliff Rhodes to Paul Druggitt <=-
 CR> Ok, you'll need more information passed to the function. Here is a C
 CR> approach that might give you some ideas. See my comment below about C
 CR> versus C++.
 CR> #include 
 CR> #include 
 CR>
 CR> int membercmp(void *struc, size_t ssize, void *data, size_t dsize,
 CR> FILE *fp)
 CR> {
 CR> /* Returns 0 if the members are equal, 1 if not.
 CR>  * !!!!!NOTE: Assumes all files are opened in binary mode!!!!!
 CR> */
 CR>
 CR> int i, ret = 0;
 CR>
 CR>    /* Get the offset of the data member in struc... */
 CR>    size_t doffset = (char *) data - (char *) struc;
 CR>
 CR>    char *s = malloc(ssize);   /* Get space to save record */
 CR>
 CR>    fread(s, ssize, 1, fp);    /* Read the record */
 CR>
 CR>    for(i = 0; i < dsize; i++)
 CR>        if(s[doffset + i] != ((char *)data)[i])
 CR>            ret = 1;
 This if(....) checks each byte for equality, which doesn't quite solve
 my problem, which is I wish see which is the lowest value.
 eg. if I have the structure
     struct SMALL_REC {
        long date;
        char desc[26];
        fpos_t prev;
        fpos_t next;
     } DATA;
     If I enter a date, converted to a long, say 727999, into DATA.date
     then read a record from the file in which the date is stored as
     728000,
     the result of a printf() is
     FROM FILE                  NEW ENTRY
     s[doffset + 0] = FFC0      ((char *)data)[0] = FFBF
     s[doffset + 1] =   1B      ((char *)data)[1] =   1B
     s[doffset + 2] =    B      ((char *)data)[2] =    B
     s[doffset + 3] =    0      ((char *)data)[3] =    0
     this                       this
     represents                 represents
     728000                     727999
     The first byte of s, (FFC0) is larger than the first byte of
     data, (FFBF) therefore the date in the file is newer than the date
     just entered. ----- NOT NECESSARILY-----.
     if the values are for example 65536 in the file, and 65535 is
     entered,
     the result of a printf() is
     FROM FILE                  NEW ENTRY
     s[doffset + 0] =    0      ((char *)data)[0] = FFFF
     s[doffset + 1] =    0      ((char *)data)[1] = FFFF
     s[doffset + 2] =    1      ((char *)data)[2] =    0
     s[doffset + 3] =    0      ((char *)data)[3] =    0
     this                       this
     represents                 represents
     65536                      65535
     The first byte of s, (0) is less than the first byte of
     data, (FFFF) therefore the date in the file is older than the date
     just entered. ------ WRONG ------
 1) How can I check properly which is REALLY the lowest value?
    whether it be int, long, double, string etc.....
    Do I somehow have to convert the 4 bytes that represent a long into
    a proper long value etc? If so, how?
 2) Once I know which is the lowest value, how do I set the prev &
    next values in the record I wish to write to the file, likewise the
    records already in the file that I need to alter the prev & next
    values, bearing in mind that within this function we are dealing
    with a void pointer to my structure DATA.
 3) Is there an easier way to add records to a file, sorted in the order
    I specify(date order, alphabetic order etc...), without having to
    rewrite the whole file. I am trying to do it as a linked list FILE
    at present.
 CR> call as
 CR>
 CR> membercmp(&DATA, sizeof(DATA), &DATA.date, sizeof(DATA.date),
 CR> fp1);
 Would I also need to pass the addresses of DATA.prev & DATA.next to
 the function?
 CR> The way you have structured your problem is not good C++.
 I know :-)
 CR> Do you intend to do this in C or C++?
 'C'
 CR> If you want to use C++ you should consider inheritance and file
 CR> streams.
 I am trying to teach myself C from books & asking questions, still very
 much a novice & nowhere near ready for C++ yet.
 CR> If you want to do it in C, you ought to switch to the C echo. :-)
 I knew I would get caught out !! I am no longer able to get the C echo
 that you lot contribute to. The one I get is UK only & no-one has been
 able to answer my question, so I tried the C++ echo in desperation,
 hoping a) to get a C answer & b) that no-one would notice. :-)
 If you don't wish to reply in this echo, I will understand. How about
 NETMAIL perhaps if needs be?
 Regards,
 Paul Druggitt.
        FIDONET NETMAIL Address   2:250/102
--- Blue Wave/Max v2.30 [NR]
---------------
* Origin: The Cavern BBS, Manchester, UK +44-61-796-1770 (2:250/102.0)

SOURCE: echomail via exec-pc

Email questions or comments to sysop@ipingthereforeiam.com
All parts of this website painstakingly hand-crafted in the U.S.A.!
IPTIA BBS/MUD/Terminal/Game Server List, © 2025 IPTIA Consulting™.