The strcpy() and strcat() functions are not working properly for me in Turbo C++. I have given my code below. I want the output to be:
C:\TURBOC3\BIN\BANK\SHOP\SELLER\334pd.txt
C:\TURBOC3\BIN\BANK\SHOP\SELLER\334pr.txt
C:\TURBOC3\BIN\BANK\SHOP\CART\311itm.txt
C:\TURBOC3\BIN\BANK\SHOP\CART\311pzr.txt
But The Output I Am Getting Is :
C:\TURBOC3\BIN\BANK\SHOP\SELLER\334pd.txt
C:\TURBOC3\BIN\BANK\SHOP\SELLER\334pr.txt
C:\TURBOC3\BIN\BANK\SHOP\CART\311itm.txt
4pd.txt
Can anyone please point out the error in my code and how to solve it?
void add_to_cart(int se_id,int c_id,char p_name[])
{
char id_seller[100],id_customer[100],id_seller1[100],id_customer1[100];
itoa(se_id,id_seller,10);
itoa(se_id,id_seller1,10);
itoa(c_id,id_customer,10);
itoa(c_id,id_customer1,10);
strcat(id_seller,"pd.txt");
strcat(id_seller1,"pr.txt");
strcat(id_customer,"itm.txt");
strcat(id_customer1,"pzr.txt");
char location_of_cart_product[]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\CART\\",location_of_cart_price[100];
char location_of_product[]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\SELLER\\",lop[100];
strcpy(location_of_cart_price,location_of_cart_product);
strcpy(lop,location_of_product);
strcat(location_of_cart_product,id_customer);
strcat(location_of_cart_price,id_customer1);
strcat(lop,id_seller1);
strcat(location_of_product,id_seller);
puts(location_of_product);
puts(lop);
puts(location_of_cart_product);
puts(location_of_cart_price);
}
These statements:
char location_of_cart_product[]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\CART\\"
char location_of_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\SELLER\\"
Allocate an array of size 33. You cannot append to this array using strcat without smashing your stack. See array initialization
You can write:
char location_of_cart_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\CART\\";
char location_of_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\SELLER\\";
Which will allocate 100 characters and initialize the first part of the array with the string.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.