Solved

Mixing Variable Values

Posted on 2004-04-01
17
250 Views
Last Modified: 2010-04-15
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!
0
Comment
Question by:sexy-sx
  • 9
  • 7
17 Comments
 
LVL 17

Expert Comment

by:mokule
ID: 10737183
What is Your data declarations?
Suspect too little memory reserved for example for orderdate
0
 

Author Comment

by:sexy-sx
ID: 10737188
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
 
LVL 17

Expert Comment

by:mokule
ID: 10737216
Looks good, but can't see where is filled for example currentorderdate and so on.
Is it too big to show it complete?
0
 
LVL 17

Expert Comment

by:mokule
ID: 10737243
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
 

Author Comment

by:sexy-sx
ID: 10737260
     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
 

Author Comment

by:sexy-sx
ID: 10737261
i'm using gcc to compile so i don't have a debugger.
0
 
LVL 17

Expert Comment

by:mokule
ID: 10737310
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
 
LVL 17

Expert Comment

by:mokule
ID: 10737325
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 17

Expert Comment

by:mokule
ID: 10737403
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
 

Author Comment

by:sexy-sx
ID: 10738206
#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
 
LVL 17

Expert Comment

by:mokule
ID: 10738839
> #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
 
LVL 20

Assisted Solution

by:jmcg
jmcg earned 37 total points
ID: 10738865
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
 

Author Comment

by:sexy-sx
ID: 10753971
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
 
LVL 17

Accepted Solution

by:
mokule earned 38 total points
ID: 10754019
Here are errors

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

Should be

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

Author Comment

by:sexy-sx
ID: 10754058
why? i don't understand
0
 
LVL 17

Expert Comment

by:mokule
ID: 10754092
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
 

Author Comment

by:sexy-sx
ID: 10754271
ok, thanks i'll do that.

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

thanks heaps!
0

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

757 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now