Thread: How do I check for a specific missing structure member?

  1. #1
    Unregistered
    Guest

    How do I check for a specific missing structure member?

    Hi,

    I'm about to start the 3rd attempt at an assignment that validates a file of 3 different record types.
    One of the checks is supposed to find a missing string of 20 characters in one of the record types. (Yes, it's a Computeach Assignment)

    I don't know if the check is just for a string of 20 spaces, ie no characters or for an actual empty string.
    I thought my code catered for both of these possibilities, I even check the size of the structure and if it's 20 bytes too small I assume the name is missing.
    However it still doesn't work!

    Any clues would be greatly appreciated!

    PS I know there's no code to go on, but it's late and I'm tired and this must have a blindingly obvious answer right?!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Personally, I'd make a tag in the record itself which denotes its type. I'd make this the first entry in the record.

    1) read the tag,
    2) backup just before the tag
    3) read the whole record based on the tag

    ie:
    Code:
    fread( &tag, sizeof(int), 1, fp );
    fseek( fp, -sizeof(int), SEEK_CUR );
    fread( tag == 1 ?
        &rec1, sizeof(rec1) : tag == 2 ?
        &rec2, sizeof(rec2) :
        &rec3, sizeof(rec3) ,
        1, fp );
    Additionally, you could stick the three records in a union, and read it that way, then validate your records once you've read them. This would mean that when you write them, in binary, the records would end up taking up the same space on disk. The benifit of this is that it provides for fast seek times / lookups.

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

  3. #3
    Registered User
    Join Date
    Jan 2002
    Posts
    16
    Sorry I should have been more specific.

    I've already written a complete, working program that uses tags to determine the record type, that can find check digit errors, wrong tag code, non-digits in a number string etc.

    What the program should also do is to find if a string is 'missing' and there is no definition of 'missing' to go on.

    The input file is opened as a .txt file and is line sequential, the record type in question contains a single character code (tag), a 5 digit code, a 20 character name, a 60 character address and a couple of numbers, all saved as strings in a structure.

    I use fgets to read the strings into the structure. I've tried using fread into the structure, but, and this is the problem, if say, the name is completely missing, then fgets( structure.name, in_fp, 20 ); will read 20 characters from the address and so on. I've written a function that takes the size of the input structure and compares it with the correct size, but this doesn't appear to work on the secret test file used by Computeach.

    I've scoured my Source material and can't find anything that even gives me a clue.

    I don't necessarily want a complete answer but a nudge in the right direction would be much appreciated!

    Cheers
    'C that?... I felt nowt' - The Original Foffo Spearjig and his hard dog. The Tube TV show, sometime in the 80's

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use *scanf to scan the string to find whatever delimits your fields. ie:

    tag,number,20char,60char

    fscanf( fp, "%[^,],", buffer );

    Then check the buffer:

    num = stlren( buf )
    switch ( num )
    {
    case 1: it's a tag; break;
    case 5: it's your code; break;
    case 20: it's your name string; break;
    case 60: it's your last field; break;
    default: it's unknown; break;
    }

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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. header file bringing errors?
    By bluehead in forum Windows Programming
    Replies: 4
    Last Post: 08-19-2003, 12:51 PM
  2. Tab Controls - API
    By -KEN- in forum Windows Programming
    Replies: 7
    Last Post: 06-02-2002, 09:44 AM
  3. odd errors from msvc std library files
    By blight2c in forum C++ Programming
    Replies: 6
    Last Post: 04-30-2002, 12:06 AM
  4. Serial Communications in C
    By ExDigit in forum Windows Programming
    Replies: 7
    Last Post: 01-09-2002, 10:52 AM
  5. structure member initialization
    By ivandn in forum C Programming
    Replies: 4
    Last Post: 10-27-2001, 01:27 PM