sexy-sx
asked on
Mixing Variable Values
Hi Experts,
I'm formatting an output file from C so i can fax it via an application called ZetaFax.
The output should look like this:
Supplier : HYDE PARK INTERNATIONAL P/L
Order Date : 01/04/2004
Store : 067-Wellington NZ
Delivery Address : 120 Hutt Street WELLINGTON
Contact Number : 0011 644 472 5600
Order No : MAKDE11
Customer Ref : E11
Order Page Number : 1
Fax Page Number : 2
Sub Dept : MAKD
but i get this:
Supplier : HYDE PARK INTERNATIONAL P/L
Order Date : 01/04/200400HYDE PARK INTERNATIONAL P/L
Store : 067-Wellington NZ
Delivery Address : 120 Hutt Street WELLINGTON
Contact Number : 0011 644 472 5600
Order No : MAKD2
Customer Ref : Normal Stock Order
Order Page Number : 2
Fax Page Number : 4
Sub Dept : MAKD
I'm reading from an output file written by MySQL.
Here's a part of the code that you might be interested in.
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
...
if (changeorderflag == 1)
{
fscanf(fpread, "%s", contactnumberstring);
x = strcmp(contactnumberstring ,END_OF_CO NTACT_NUMB ER);
while (x != 0)
{
strcat(contactnumber,conta ctnumberst ring);
strcat(contactnumber," ");
fscanf(fpread, "%s", contactnumberstring);
x = strcmp(contactnumberstring ,END_OF_CO NTACT_NUMB ER);
}
strcpy(currentcontactnumbe r,contactn umber);
}
else
{
strcpy(orderdate,currentor derdate);
strcpy(suppliername,curren tsuppliern ame);
strcpy(storename,currentst orename);
strcpy(address,currentaddr ess);
strcpy(ordernumber,current ordernumbe r);
strcpy(customer,currentcus tomer);
strcpy(subdeptstr,currents ubdeptstr) ;
strcpy(contactnumber,curre ntcontactn umber);
}
...
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
fprintf(orderswrite, "Supplier : %s\n",suppliername); /*001*/
fprintf(orderswrite, "Order Date : %s\n",orderdate); /*001*/
fprintf(orderswrite, "Store : %s\n",storename);
strcpy(storename,SPACES);
fprintf(orderswrite, "Delivery Address : %s\n",address);
strcpy(address,SPACES);
fprintf(orderswrite, "Contact Number : %s\n",contactnumber); /*002*/
strcpy(contactnumber,SPACE S);
fprintf(orderswrite, "Order No : %s\n",ordernumber);
strcpy(ordernumber,SPACES) ;
fprintf(orderswrite, "Customer Ref : %s\n",customer);
strcpy(customer,SPACES);
fprintf(orderswrite, "Order Page Number : %d\n",orderpagenumber);
fprintf(orderswrite, "Fax Page Number : %d\n",faxpagenumber);
fprintf(orderswrite, "Sub Dept : %s\n\n",subdeptstr);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE1);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE2);
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
Why is the Order date getting mixed up with the supplier name?
When i change the order of the variables in the else statement, the order date picks up another variable e.g. Contact Number.
What am i doing wrong?
Thanks in advanced!
I'm formatting an output file from C so i can fax it via an application called ZetaFax.
The output should look like this:
Supplier : HYDE PARK INTERNATIONAL P/L
Order Date : 01/04/2004
Store : 067-Wellington NZ
Delivery Address : 120 Hutt Street WELLINGTON
Contact Number : 0011 644 472 5600
Order No : MAKDE11
Customer Ref : E11
Order Page Number : 1
Fax Page Number : 2
Sub Dept : MAKD
but i get this:
Supplier : HYDE PARK INTERNATIONAL P/L
Order Date : 01/04/200400HYDE PARK INTERNATIONAL P/L
Store : 067-Wellington NZ
Delivery Address : 120 Hutt Street WELLINGTON
Contact Number : 0011 644 472 5600
Order No : MAKD2
Customer Ref : Normal Stock Order
Order Page Number : 2
Fax Page Number : 4
Sub Dept : MAKD
I'm reading from an output file written by MySQL.
Here's a part of the code that you might be interested in.
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
...
if (changeorderflag == 1)
{
fscanf(fpread, "%s", contactnumberstring);
x = strcmp(contactnumberstring
while (x != 0)
{
strcat(contactnumber,conta
strcat(contactnumber," ");
fscanf(fpread, "%s", contactnumberstring);
x = strcmp(contactnumberstring
}
strcpy(currentcontactnumbe
}
else
{
strcpy(orderdate,currentor
strcpy(suppliername,curren
strcpy(storename,currentst
strcpy(address,currentaddr
strcpy(ordernumber,current
strcpy(customer,currentcus
strcpy(subdeptstr,currents
strcpy(contactnumber,curre
}
...
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
fprintf(orderswrite, "Supplier : %s\n",suppliername); /*001*/
fprintf(orderswrite, "Order Date : %s\n",orderdate); /*001*/
fprintf(orderswrite, "Store : %s\n",storename);
strcpy(storename,SPACES);
fprintf(orderswrite, "Delivery Address : %s\n",address);
strcpy(address,SPACES);
fprintf(orderswrite, "Contact Number : %s\n",contactnumber); /*002*/
strcpy(contactnumber,SPACE
fprintf(orderswrite, "Order No : %s\n",ordernumber);
strcpy(ordernumber,SPACES)
fprintf(orderswrite, "Customer Ref : %s\n",customer);
strcpy(customer,SPACES);
fprintf(orderswrite, "Order Page Number : %d\n",orderpagenumber);
fprintf(orderswrite, "Fax Page Number : %d\n",faxpagenumber);
fprintf(orderswrite, "Sub Dept : %s\n\n",subdeptstr);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE1);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE2);
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
Why is the Order date getting mixed up with the supplier name?
When i change the order of the variables in the else statement, the order date picks up another variable e.g. Contact Number.
What am i doing wrong?
Thanks in advanced!
ASKER
int loopcount;
char ordernumber [30] = "\0";
static char currentordernumber [30];
char storestring [50] = "\0";
char storename [50] = "\0";
static char currentstorename [50];
char addressstring [50] = "\0";
char address [150] = "\0";
static char currentaddress [150];
char subdeptstr [10] = "\0";
static char currentsubdeptstr [10];
char orderstring [30] = "\0";
char customer [30] = "\0";
static char currentcustomer [30];
char customerstring [30] = "\0";
int x=0;
char supplierstring [50] = "\0";
char suppliername [150] = "\0";
static char currentsuppliername [150] = "\0";
char orderdate [10] = "\0";
static char currentorderdate [10] = "\0";
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
char ordernumber [30] = "\0";
static char currentordernumber [30];
char storestring [50] = "\0";
char storename [50] = "\0";
static char currentstorename [50];
char addressstring [50] = "\0";
char address [150] = "\0";
static char currentaddress [150];
char subdeptstr [10] = "\0";
static char currentsubdeptstr [10];
char orderstring [30] = "\0";
char customer [30] = "\0";
static char currentcustomer [30];
char customerstring [30] = "\0";
int x=0;
char supplierstring [50] = "\0";
char suppliername [150] = "\0";
static char currentsuppliername [150] = "\0";
char orderdate [10] = "\0";
static char currentorderdate [10] = "\0";
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
Looks good, but can't see where is filled for example currentorderdate and so on.
Is it too big to show it complete?
Is it too big to show it complete?
Stop the the program in debugger in places where You fill orderdate.
There are two possibilities either You filled it bad or overwrite later terminating 0;
There are two possibilities either You filled it bad or overwrite later terminating 0;
ASKER
if (changeorderflag == 1)
{
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_ OF_SUPPLIE R_NAME); /*001*/
while (x != 0) /*001*/
{ /*001*/
strcat(suppliername,suppli erstring); /*001*/
strcat(suppliername," "); /*001*/
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_ OF_SUPPLIE R_NAME); /*001*/
} /*001*/
strcpy(currentsuppliername ,suppliern ame); /*001*/
fscanf(fpread, "%s", orderdate); /*001*/
strcpy(currentorderdate,or derdate); /*001*/
fscanf(fpread, "%s", storestring);
x = strcmp(storestring,END_OF_ STORE);
while (x != 0)
{
strcat(storename,storestri ng);
strcat(storename," ");
fscanf(fpread, "%s", storestring);
x = strcmp(storestring,END_OF_ STORE);
}
strcpy(currentstorename,st orename);
fscanf(fpread, "%s", addressstring);
x = strcmp(addressstring,END_O F_ADDRESS) ;
while (x != 0)
{
strcat(address,addressstri ng);
strcat(address," ");
fscanf(fpread, "%s", addressstring);
x = strcmp(addressstring,END_O F_ADDRESS) ;
}
strcpy(currentaddress,addr ess);
fscanf(fpread, "%s", contactnumberstring); /*002*/
x = strcmp(contactnumberstring ,END_OF_CO NTACT_NUMB ER); /*002*/
while (x != 0) /*002*/
{ /*002*/
strcat(contactnumber,conta ctnumberst ring); /*002*/
strcat(contactnumber," "); /*002*/
fscanf(fpread, "%s", contactnumberstring); /*002*/
x = strcmp(contactnumberstring ,END_OF_CO NTACT_NUMB ER); /*002*/
} /*002*/
strcpy(currentcontactnumbe r,contactn umber); /*002*/
fscanf(fpread, "%s", orderstring);
x = strcmp(orderstring,END_OF_ ORDER_NUMB ER);
while (x != 0)
{
strcat(ordernumber,orderst ring);
strcat(ordernumber," ");
fscanf(fpread, "%s", orderstring);
x = strcmp(orderstring,END_OF_ ORDER_NUMB ER);
}
strcpy(currentordernumber, ordernumbe r);
fscanf(fpread, "%s", customerstring);
x = strcmp(customerstring,END_ OF_CUSTOME R);
while (x != 0)
{
strcat(customer,customerst ring);
strcat(customer," ");
fscanf(fpread, "%s", customerstring);
x = strcmp(customerstring,END_ OF_CUSTOME R);
}
strcpy(currentcustomer,cus tomer);
fscanf(fpread, "%s", subdeptstr);
strcpy(currentsubdeptstr,s ubdeptstr) ;
}
else
{
strcpy(orderdate,currentor derdate); /*001*/
strcpy(suppliername,curren tsuppliern ame); /*001*/
strcpy(storename,currentst orename);
strcpy(address,currentaddr ess);
strcpy(ordernumber,current ordernumbe r);
strcpy(customer,currentcus tomer);
strcpy(subdeptstr,currents ubdeptstr) ;
strcpy(contactnumber,curre ntcontactn umber); /*002*/
}
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
fprintf(orderswrite, "Supplier : %s\n",suppliername); /*001*/
fprintf(orderswrite, "Order Date : %s\n",orderdate); /*001*/
fprintf(orderswrite, "Store : %s\n",storename);
strcpy(storename,SPACES);
fprintf(orderswrite, "Delivery Address : %s\n",address);
strcpy(address,SPACES);
fprintf(orderswrite, "Contact Number : %s\n",contactnumber); /*002*/
strcpy(contactnumber,SPACE S);
fprintf(orderswrite, "Order No : %s\n",ordernumber);
strcpy(ordernumber,SPACES) ;
fprintf(orderswrite, "Customer Ref : %s\n",customer);
strcpy(customer,SPACES);
fprintf(orderswrite, "Order Page Number : %d\n",orderpagenumber);
fprintf(orderswrite, "Fax Page Number : %d\n",faxpagenumber);
fprintf(orderswrite, "Sub Dept : %s\n\n",subdeptstr);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE1);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE2);
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
*linecount = *linecount + 23;
}
{
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_
while (x != 0) /*001*/
{ /*001*/
strcat(suppliername,suppli
strcat(suppliername," "); /*001*/
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_
} /*001*/
strcpy(currentsuppliername
fscanf(fpread, "%s", orderdate); /*001*/
strcpy(currentorderdate,or
fscanf(fpread, "%s", storestring);
x = strcmp(storestring,END_OF_
while (x != 0)
{
strcat(storename,storestri
strcat(storename," ");
fscanf(fpread, "%s", storestring);
x = strcmp(storestring,END_OF_
}
strcpy(currentstorename,st
fscanf(fpread, "%s", addressstring);
x = strcmp(addressstring,END_O
while (x != 0)
{
strcat(address,addressstri
strcat(address," ");
fscanf(fpread, "%s", addressstring);
x = strcmp(addressstring,END_O
}
strcpy(currentaddress,addr
fscanf(fpread, "%s", contactnumberstring); /*002*/
x = strcmp(contactnumberstring
while (x != 0) /*002*/
{ /*002*/
strcat(contactnumber,conta
strcat(contactnumber," "); /*002*/
fscanf(fpread, "%s", contactnumberstring); /*002*/
x = strcmp(contactnumberstring
} /*002*/
strcpy(currentcontactnumbe
fscanf(fpread, "%s", orderstring);
x = strcmp(orderstring,END_OF_
while (x != 0)
{
strcat(ordernumber,orderst
strcat(ordernumber," ");
fscanf(fpread, "%s", orderstring);
x = strcmp(orderstring,END_OF_
}
strcpy(currentordernumber,
fscanf(fpread, "%s", customerstring);
x = strcmp(customerstring,END_
while (x != 0)
{
strcat(customer,customerst
strcat(customer," ");
fscanf(fpread, "%s", customerstring);
x = strcmp(customerstring,END_
}
strcpy(currentcustomer,cus
fscanf(fpread, "%s", subdeptstr);
strcpy(currentsubdeptstr,s
}
else
{
strcpy(orderdate,currentor
strcpy(suppliername,curren
strcpy(storename,currentst
strcpy(address,currentaddr
strcpy(ordernumber,current
strcpy(customer,currentcus
strcpy(subdeptstr,currents
strcpy(contactnumber,curre
}
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
fprintf(orderswrite, "Supplier : %s\n",suppliername); /*001*/
fprintf(orderswrite, "Order Date : %s\n",orderdate); /*001*/
fprintf(orderswrite, "Store : %s\n",storename);
strcpy(storename,SPACES);
fprintf(orderswrite, "Delivery Address : %s\n",address);
strcpy(address,SPACES);
fprintf(orderswrite, "Contact Number : %s\n",contactnumber); /*002*/
strcpy(contactnumber,SPACE
fprintf(orderswrite, "Order No : %s\n",ordernumber);
strcpy(ordernumber,SPACES)
fprintf(orderswrite, "Customer Ref : %s\n",customer);
strcpy(customer,SPACES);
fprintf(orderswrite, "Order Page Number : %d\n",orderpagenumber);
fprintf(orderswrite, "Fax Page Number : %d\n",faxpagenumber);
fprintf(orderswrite, "Sub Dept : %s\n\n",subdeptstr);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE1);
fprintf(orderswrite, "%s\n", ORDER_MESSAGE2);
print_line(orderswrite);
fprintf(orderswrite, "\n\n");
*linecount = *linecount + 23;
}
ASKER
i'm using gcc to compile so i don't have a debugger.
1. There are debuggers: gdb or ddd
2. In a moment You can try to make test fprint of variable orderdate in some places from filling to real fprintf, finding where it is damaged.
2. In a moment You can try to make test fprint of variable orderdate in some places from filling to real fprintf, finding where it is damaged.
Hey, but from the beggining. What about suppliername. First instruction is strcat ?
if (changeorderflag == 1)
{
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_ OF_SUPPLIE R_NAME); /*001*/
while (x != 0) /*001*/
{ /*001*/
->>> strcat(suppliername,suppli erstring); /*001*/
strcat(suppliername," "); /*001*/
if (changeorderflag == 1)
{
fscanf(fpread, "%s", supplierstring); /*001*/
x = strcmp(supplierstring,END_
while (x != 0) /*001*/
{ /*001*/
->>> strcat(suppliername,suppli
strcat(suppliername," "); /*001*/
Last question what is SPACES. It seems like You fill with it different in length fields. Look at it
I'm sorry but I'll be off now.
I'm sorry but I'll be off now.
ASKER
#define SPACES ""
it filles the variable with a blank space.
i don't understand what you mean by the previous question.
The output file looks a little like this Supplier Name<EOS>
It looks for the <EOS> to exit the loop, and it concats the Supplier and Name, because we are only retrieving a string on it's own.
e.g. 1st time in look Supplier, 2nd time in look Name.
Join Supplier Name
Is this too confusing?
it filles the variable with a blank space.
i don't understand what you mean by the previous question.
The output file looks a little like this Supplier Name<EOS>
It looks for the <EOS> to exit the loop, and it concats the Supplier and Name, because we are only retrieving a string on it's own.
e.g. 1st time in look Supplier, 2nd time in look Name.
Join Supplier Name
Is this too confusing?
> #define SPACES ""
OK
I just imagine according to name of that constant that it is longer string.
>Is this too confusing?
No. But I just don't see an initializition of suppliername and so on.
Such as strcpy(suppliername,"");
What I see is that You concatening (strcat) to something what resides there already.
It applies to more variables.
OK
I just imagine according to name of that constant that it is longer string.
>Is this too confusing?
No. But I just don't see an initializition of suppliername and so on.
Such as strcpy(suppliername,"");
What I see is that You concatening (strcat) to something what resides there already.
It applies to more variables.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Ok,
I have kinda sorted it out. It's not the proper way of doing it.
But i've initialized all the static chars with nulls and that sorted it out.
jmcg - The reason i'm reading string by string's because a whole line looks like this:
<SOO> 1MFCHA MICHAEL S FASHION IMPORTS P/L <EOSN> 02/04/2004 067-Wellington NZ <EOS> 120 Hutt Street WELLINGTON <EOA> 0011 644 472 5600 <EOCN> MAKDE11 <EOON> E11 <EOC> MAKD
It writes the string to the variable until it find the <ID>
I don't know why i had to initialize the statics though, i also had to move these:
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
above
char orderdate [10] = "\0";
static char currentorderdate [10] = "\0";
weird...!
if anyone knows why c is doing this, please tell me and i'll award you the points.
I have kinda sorted it out. It's not the proper way of doing it.
But i've initialized all the static chars with nulls and that sorted it out.
jmcg - The reason i'm reading string by string's because a whole line looks like this:
<SOO> 1MFCHA MICHAEL S FASHION IMPORTS P/L <EOSN> 02/04/2004 067-Wellington NZ <EOS> 120 Hutt Street WELLINGTON <EOA> 0011 644 472 5600 <EOCN> MAKDE11 <EOON> E11 <EOC> MAKD
It writes the string to the variable until it find the <ID>
I don't know why i had to initialize the statics though, i also had to move these:
char contactnumber [20] = "\0";
char contactnumberstring [20] = "\0";
static char currentcontactnumber [20] = "\0";
above
char orderdate [10] = "\0";
static char currentorderdate [10] = "\0";
weird...!
if anyone knows why c is doing this, please tell me and i'll award you the points.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
why? i don't understand
Because Your date format is 01/04/2004 which consist of 10 characters.
You must store there one more character that is "\0" which means end of string.
You must be aware of necessity of ending each string with \0
For example instruction
strcpy(dst,src) copies characters untill it finds ending \0 (which is also copied)
You must store there one more character that is "\0" which means end of string.
You must be aware of necessity of ending each string with \0
For example instruction
strcpy(dst,src) copies characters untill it finds ending \0 (which is also copied)
ASKER
ok, thanks i'll do that.
i'll also award mokule and jmcg the points for your help.
thanks heaps!
i'll also award mokule and jmcg the points for your help.
thanks heaps!
Suspect too little memory reserved for example for orderdate