Link to home
Start Free TrialLog in
Avatar of sexy-sx
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_CONTACT_NUMBER);
      while (x != 0)
      {
            strcat(contactnumber,contactnumberstring);
            strcat(contactnumber," ");
            fscanf(fpread, "%s", contactnumberstring);
            x = strcmp(contactnumberstring,END_OF_CONTACT_NUMBER);
      }
      strcpy(currentcontactnumber,contactnumber);
}
else
{
      strcpy(orderdate,currentorderdate);
      strcpy(suppliername,currentsuppliername);
      strcpy(storename,currentstorename);
      strcpy(address,currentaddress);
      strcpy(ordernumber,currentordernumber);
      strcpy(customer,currentcustomer);
      strcpy(subdeptstr,currentsubdeptstr);
      strcpy(contactnumber,currentcontactnumber);
}

...

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,SPACES);
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!
Avatar of mokule
mokule
Flag of Poland image

What is Your data declarations?
Suspect too little memory reserved for example for orderdate
Avatar of sexy-sx
sexy-sx

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";
Looks good, but can't see where is filled for example currentorderdate and so on.
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;
Avatar of sexy-sx

ASKER

     if (changeorderflag == 1)
            {
            fscanf(fpread, "%s", supplierstring);                        /*001*/
            x = strcmp(supplierstring,END_OF_SUPPLIER_NAME);            /*001*/
            while (x != 0)                                          /*001*/
            {                                                /*001*/
                  strcat(suppliername,supplierstring);                  /*001*/
                  strcat(suppliername," ");                        /*001*/
                  fscanf(fpread, "%s", supplierstring);                  /*001*/
                  x = strcmp(supplierstring,END_OF_SUPPLIER_NAME);      /*001*/
            }                                                /*001*/
            strcpy(currentsuppliername,suppliername);                  /*001*/
            fscanf(fpread, "%s", orderdate);                        /*001*/
            strcpy(currentorderdate,orderdate);                        /*001*/
            fscanf(fpread, "%s", storestring);
            x = strcmp(storestring,END_OF_STORE);
            while (x != 0)
            {
                  strcat(storename,storestring);
                  strcat(storename," ");
                  fscanf(fpread, "%s", storestring);
                  x = strcmp(storestring,END_OF_STORE);
            }
            strcpy(currentstorename,storename);
            fscanf(fpread, "%s", addressstring);
            x = strcmp(addressstring,END_OF_ADDRESS);
            while (x != 0)
            {
                  strcat(address,addressstring);
                  strcat(address," ");
                  fscanf(fpread, "%s", addressstring);
                  x = strcmp(addressstring,END_OF_ADDRESS);
            }
            strcpy(currentaddress,address);
            fscanf(fpread, "%s", contactnumberstring);                        /*002*/
            x = strcmp(contactnumberstring,END_OF_CONTACT_NUMBER);                        /*002*/
            while (x != 0)                        /*002*/
            {                        /*002*/
                  strcat(contactnumber,contactnumberstring);                        /*002*/
                  strcat(contactnumber," ");                        /*002*/
                  fscanf(fpread, "%s", contactnumberstring);                        /*002*/
                  x = strcmp(contactnumberstring,END_OF_CONTACT_NUMBER);                        /*002*/
            }                        /*002*/
            strcpy(currentcontactnumber,contactnumber);                        /*002*/
            fscanf(fpread, "%s", orderstring);
            x = strcmp(orderstring,END_OF_ORDER_NUMBER);
            while (x != 0)
            {
                  strcat(ordernumber,orderstring);
                  strcat(ordernumber," ");
                  fscanf(fpread, "%s", orderstring);
                  x = strcmp(orderstring,END_OF_ORDER_NUMBER);
            }
            strcpy(currentordernumber,ordernumber);
            fscanf(fpread, "%s", customerstring);
            x = strcmp(customerstring,END_OF_CUSTOMER);
            while (x != 0)
            {
                  strcat(customer,customerstring);
                  strcat(customer," ");
                  fscanf(fpread, "%s", customerstring);
                  x = strcmp(customerstring,END_OF_CUSTOMER);
            }
            strcpy(currentcustomer,customer);
            fscanf(fpread, "%s", subdeptstr);
            strcpy(currentsubdeptstr,subdeptstr);
      }
      else
      {
            strcpy(orderdate,currentorderdate);                        /*001*/
            strcpy(suppliername,currentsuppliername);                  /*001*/
            strcpy(storename,currentstorename);
            strcpy(address,currentaddress);
            strcpy(ordernumber,currentordernumber);
            strcpy(customer,currentcustomer);
            strcpy(subdeptstr,currentsubdeptstr);
            strcpy(contactnumber,currentcontactnumber);            /*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,SPACES);
      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;
}
Avatar of sexy-sx

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.
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_SUPPLIER_NAME);          /*001*/
          while (x != 0)                                   /*001*/
          {                                        /*001*/
->>>               strcat(suppliername,supplierstring);               /*001*/
               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.
Avatar of sexy-sx

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?
> #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.

SOLUTION
Avatar of jmcg
jmcg
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sexy-sx

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.
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sexy-sx

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)
Avatar of sexy-sx

ASKER

ok, thanks i'll do that.

i'll also award mokule and jmcg the points for your help.

thanks heaps!