OK, thanks for all your help; I have two obstacles,
I'm amending someone else's code (which I'm assuming is correct) and my own lack of experience in
real life C programming.
I'll definitely look at the pointer initialization and perhaps not even use a pointer there.
Looking over my post again, I realized one of my main worries is when I retrieve CcyDecpl why is
it giving me three characters instead of one?
Firstly, a bit more info on the program. I'll reflect on posting the whole program, but feel
it is too large and, anyway, cannot be run without the appropriate Btrieve files. Hence I am
striving to give all relevant information. The program reads a GL file and for each GL record
extracts additional information, such as from the Currency file, before writing to an output file.
Getting back to my Currency structure, I declared it so
Code:
struct ccyRec
{
char CcyRtypeXC [2];
char CcyRtype00 [2];
char Ccycode [2];
char Ccysymbol [3];
char CcyDecpl [1];
};
The pointer is declared with
Code:
struct ccyRec CCY_REC_BUF, *pq;
My understanding is that this is declaring two things; an instance of structure ccyRec called CCY_REC_BUF,
and a pointer pq.
pq is inialized (i.e. pointed at the structure)
The file is opened (not shown) and at the appropriate times this loop is used to find the correct record
(matching Ccycode) and the corresponding value of Ccysymbol is saved. The Btrieve record is read into CCY_REC_BUF.
Code:
/*================================================================*/
/* Check CcyCode = Limit Currency, as per Join in original 2A.c */
/*================================================================*/
cStatus = BTRV (B_GETFST, CCY_POS_BLK, &CCY_REC_BUF, &CCY_BUF_LEN, CCY_KEY_BUF, 0);
strncpy(str4, "XXX", 3);
while (cStatus != EOF_ERR)
{
if (cStatus != 0)
{
emessage("Reading","CCYTABLE.BTR",cStatus);
exit(2);
}
strncpy (tst1, pi->GLDAc_cc, 2);
strncpy (tst2, pq->Ccycode, 2);
if (strncmp(tst2, tst1, 2) == 0)
{
memcpy (str4, pq->Ccysymbol, 3);
cStatus = EOF_ERR;
}
else
{
cStatus = BTRV (B_GETNX, CCY_POS_BLK, &CCY_REC_BUF, &CCY_BUF_LEN, CCY_KEY_BUF, 0);
}
}
Assuming a matching record is found in the Currency file, then processing continues...
Code:
/*================================================================*/
/* Continue if CCYTABLE lookup is successful */
/*================================================================*/
if (strncmp(str4,"XXX",3) == 0)
{
goto getnxt;
}
<snip>
/* 4. Round off the report balance */
strcpy (a_precision, pq->CcyDecpl);
i_precision = atoi(a_precision);
if ( i_precision > 99)
i_precision /= 100;
else if ( i_precision > 9)
i_precision /= 10;
strncpy (inputAmt, po->Rept_bal, 14);
if ( i_precision == 0)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt) );
else if (i_precision == 1)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt)/10);
else if (i_precision == 2)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt)/100);
else if (i_precision == 3)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt)/1000);
else if (i_precision == 4)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt)/10000);
else if (i_precision == 5)
sprintf (outputAmt, "%.*lf", 0, atof(inputAmt)/100000);
else
{
emessage ("Wrong currency decimal place", "Rept bal", a_precision);
}
/* copy converted Report bal amount to output buffer */
strncpy (po->Rept_bal, outputAmt, 14);
/* ============================================================================================================= */
/* WRITE OUTPUT */
STATUS = BTRV (B_INSERT, OUT_POS_BLK, &OUT_BUF, &OUT_BUF_LEN, OUT_KEY_BUF, 0);
if (STATUS != 0)
{
emessage("Writing","ICRSEX2A.BTR",STATUS);
exit(2);
}
++count;
if ( count % 10 == 0 )
{
printf (" \r");
printf ("%d record(s) being written. \r", count);
}
getnxt:
/* READ NEXT */
STATUS = BTRV (B_GETNX, GLD_POS_BLK, &GLD_REC_BUF, &GLD_BUF_LEN, GLD_KEY_BUF, 0);
}
printf ("%d record(s) written. \n", count);
In the above we have the issues with a_precision, etc., regarding initialization of course.
When it reports a wrong decimal place (incorrectly at the moment!) I display several values on screen,
as shown in my original post. The specific one I'm concerned about is
Code:
printf ("CcyDecpl = %s \n", pq->CcyDecpl);
because its output is "006" when I am expecting "0" and this is what is causing me my main problem.
I understand "pq->CcyDecpl" to mean I am dereferencing the pointer pq to the member CcyDecpl of the
structure CCY_REC_BUF (which is an instance of ccyRec). i.e. It is "looking at" the contents of the structure, populated by the last record read. The particular structure member is declared as "char CcyDecpl [1];"
so where do the other two characters come from?