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*/