• C

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!
sexy-sxAsked:
Who is Participating?
 
mokuleCommented:
Here are errors

char orderdate [10] = "\0";
static char currentorderdate [10] = "\0";

Should be

char orderdate [11] = "\0";
static char currentorderdate [11] = "\0";
0
 
mokuleCommented:
What is Your data declarations?
Suspect too little memory reserved for example for orderdate
0
 
sexy-sxAuthor Commented:
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";
0
Network Scalability - Handle Complex Environments

Monitor your entire network from a single platform. Free 30 Day Trial Now!

 
mokuleCommented:
Looks good, but can't see where is filled for example currentorderdate and so on.
Is it too big to show it complete?
0
 
mokuleCommented:
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;
0
 
sexy-sxAuthor Commented:
     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;
}
0
 
sexy-sxAuthor Commented:
i'm using gcc to compile so i don't have a debugger.
0
 
mokuleCommented:
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.
0
 
mokuleCommented:
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*/
 
0
 
mokuleCommented:
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.
0
 
sexy-sxAuthor Commented:
#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?
0
 
mokuleCommented:
> #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.

0
 
jmcgOwnerCommented:
I don't have an explanation for the entire problem you are seeing, but I have a few observations that may help.

1) You appear to have declared your orderdate fields as 10 bytes and the data you intend to store in them is a string containing 11 bytes (10 bytes of date plus a terminating \0). This is likely to cause the data in the whatever variable is stored next in memory to be corrupted.

2) Everywhere that you use strcpy should be examined to see if you can use strncpy instead. The size specified should be the size of the DESTINATION char array, it would defeat the purpose to try to use the length of the source string.

3) All of your uses of fscanf, strcat, etc are potential hazards for overrunning the allocated space in the destination string.

4) It seems silly to use fscanf with a format string of "%s" to read one word at a time, then join them together, when a simple fgets would read the whole line at once. But if you switch to using fgets, you have to remember to strip off the line termination.
0
 
sexy-sxAuthor Commented:
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.
0
 
sexy-sxAuthor Commented:
why? i don't understand
0
 
mokuleCommented:
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)
0
 
sexy-sxAuthor Commented:
ok, thanks i'll do that.

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

thanks heaps!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.