Thread: behind and confused

  1. #1
    Registered User
    Join Date
    Nov 2001
    Posts
    3

    behind and confused

    I am reading one record at a time from an invalidated line sequential file. The records need to validated and written to a new file in preparation for sorting by customer code. As the records are of different length ( I have 4 types of record I and R being the same length ) would I be better declaring 3 different arrays of the correct size, into which I can store the appropriate record, or maybe storing the record straight into the structure and then validate it. At the moment I have declared an array of undefined size to accommodate any record, but, as I have omitted the size I need to initialize it at the time of declaration otherwise it becomes illegal.
    An example of a record would be I345199286410257
    One problem is that I do not know what type of record is next so by using fscanf and storing it straight to an array, or into a structure does not seem possible.

    Below is a sample of storing the record into the appropriate structure and then writing it to the validated file. Omitted is the actual reading of the record into the array called ‘record’

    I am not looking for a computeach student (like myself) to do this for me, it's just that my head is pounding and I don't have a decent compiler! I hope someone can make sense of it.

    if ( record == ‘I’ || record ==’R’ ) /* record being an array that the actual record is stored in*/
    {
    m_1 = (record[1] * 5);
    m_2 = (record[2] * 4);
    m_3 = (record[3] * 3);
    m_4 = (record[4] * 2);
    m_5 = m_1 + m_2 + m_3 + m_4;
    check_digit = 11 – (m_5 % 11);

    if (check_digit == 11)
    check_digit = ‘X’;
    if (check_digit == 10)
    check_digit = ‘0’;

    if (check_digit != record[5] )
    {
    err_msg (“\a RECORD %s NOT VALID !!\n
    INCORRECT CHECK DIGIT !!\n”, record);
    break;
    }

    m_1 = record[6] * 6;
    m_2 = record[7] * 5;
    m_3 = record[8] * 4;
    m_4 = record[9] * 3;
    m_5 = record[10] * 2;
    m_6 = m_1 + m_2 + m_3 + m_4 + m_5;
    check_digit = 11 – (m_6 % 11);

    if (check_digit == 11)
    check_digit = ‘X’;
    if (check_digit == 10)
    check_digit = ‘0’;

    if (check_digit != record[11] )
    {
    err_msg (“\a RECORD %s NOT VALID !!\n
    INCORRECT CHECK DIGIT !!\n”, record);
    break;
    }
    /*CHECK THE ISSUE NUMBER IS NUMERIC*/
    for (i = 12, i < = 15, i ++)
    {
    if (record [i] ! > = ‘0’ || record [i] ! < = ‘9’)
    {
    err_msg (“\a RECORD %s NOT VALID !!\n
    ISSUE QUANTITY NOT NUMERIC !!\n”, record);
    break;
    }
    }

    /*copy the record to the stucture*/
    strcpy ( i_r.rec_type, record[0] );
    /*obtain customer code from record*/
    for ( i = 1, i < = 5, i ++ )
    {
    string1 [ i -1] = record [ i ];
    string1 [ i ] = ‘\O’;
    strcpy ( i_r.cust_code, string1 [ 6 ] ); /*string1 is a char string*/
    }
    /*obtain part number from record*/
    for ( i = 6, i < = 11, i ++ )
    {
    string1 [ i – 6 ] = record [ i ];
    string1 [ i ] = ‘\O’;
    strcpy ( i_r.part_no, string1 [ 7 ] );
    }
    /*obtain issue/receipt quantity from record*/
    for ( i =12, i < = 15, i ++ )
    {
    string2 [ i – 12 ] = record [ i ];
    string2 [ i ] = ‘\O’;
    strcpy ( i_r.i_r_quantity, string2 [ 5 ] ); /*string2 is a int string*/
    }
    /*write the record to file. The file would already have been opened*/

    if ( fwrite( &i_r, sizeof (struct issue_receipt), 1, v_f_ptr )
    !=1 )
    {
    err_msg (“\a ERROR DURING WRITING RECORD %s!\n
    PROGRAM TERMINATED!\n”, record);
    exit(1);
    }
    }/*end of if ISSUE_RECEIPT*/

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > One problem is that I do not know what type of record is next so by using fscanf
    Bad idea - seriously bad idea.

    This is how you should read the file
    Code:
    // assuming FILE *fp is set up to read from your data file
    char buff[BUFSIZ];  // BUFSIZ in stdio.h
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        // read the notes below
    }
    Having got the line (say "I345199286410257\n") safely read into buff, you can have several goes at determining if its a valid record.

    You might try writing some functions, like so
    &nbsp; int is_valid_r_record ( char *buff );
    &nbsp; int is_valid_i_record ( char *buff );
    These would check the start char, length, numeric content or whatever. And they can be improved separately as you think of additional tests to conduct on the records.

    The code in the above while loop might then look something like this
    Code:
    while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
        if ( is_valid_r_record( buff ) ) {
            // do something with valid r record
        } else
        if ( is_valid_i_record( buff ) ) {
            // ditto
        } else {
            // invalid record error message or whatever
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed