strcpy()和strcat()在Turbo C ++中无法正常工作(从字符串litteral初始化数组时)

问题描述 投票:-2回答:1

在Turbo C ++中,strcpy()和strcat()函数无法正常工作。我在下面给出了我的代码。我希望输出为:

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  

但我得到的输出是:

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  

任何人都可以指出我的代码中的错误以及如何解决它?

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);
}
c++ turbo-c++
1个回答
4
投票

这些陈述:

char location_of_cart_product[]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\CART\\"
char location_of_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\SELLER\\"

分配一个大小为33的数组。您不能使用strcat追加到此数组而不会粉碎堆栈。见array initialization

你可以写:

char location_of_cart_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\CART\\";
char location_of_product[100]="C:\\TURBOC3\\BIN\\BANK\\SHOP\\SELLER\\";

这将分配100个字符并使用字符串初始化数组的第一部分。

© www.soinside.com 2019 - 2024. All rights reserved.