我有函数,可以保存参数中的值。而且我必须实现两种方式,接受输入 - 作为 achar*
然后通过strncpy
。
IE: a . Add("123456/7890", "John", "Doe", "2000-01-01", "Main street", "Seattle");
它工作正常,直到我使用 strncpy:
bool status;
char lID[12], lDate[12], lName[50], lSurname[50], lStreet[50], lCity[50];
strncpy(lID, "123456/7890", sizeof ( lID));
strncpy(lName, "John", sizeof ( lName));
strncpy(lSurname, "Doe", sizeof ( lSurname));
strncpy(lDate, "2000-01-01", sizeof ( lDate));
strncpy(lStreet, "Main street", sizeof ( lStreet));
strncpy(lCity, "Seattle", sizeof ( lCity));
status = c . Add(lID, lName, lSurname, lDate, lStreet, lCity);
//is true
strncpy(lID, "987654/3210", sizeof ( lID));
strncpy(lName, "Freddy", sizeof ( lName));
strncpy(lSurname, "Kruger", sizeof ( lSurname));
strncpy(lDate, "2001-02-03", sizeof ( lDate));
strncpy(lStreet, "Elm street", sizeof ( lStreet));
strncpy(lCity, "Sacramento", sizeof ( lCity));
// notice, that I don't even save it at this point
strncpy(lID, "123456/7890", sizeof ( lID));
strncpy(lDate, "2002-12-05", sizeof ( lDate));
strncpy(lStreet, "Sunset boulevard", sizeof ( lStreet));
strncpy(lCity, "Los Angeles", sizeof ( lCity));
status = c . Resettle(lID, lDate, lStreet, lCity);
status = c . Print(cout, "123456/7890");
//is true
此时我想打印出 ID 123456/7890 的值...所以 Name:John, Surname:Doe 等 Neverthless 它打印出值,这些值被保存为最后一个:
123456/7890 Freddy Kruger
2002-12-05 Sunset boulevard Los Angeles
2002-12-05 Sunset boulevard Los Angeles
我Add
的声明为:
bool Add(const char * id,
const char * name,
const char * surname,
const char * date,
const char * street,
const char * city);
Resettle
函数声明类似于Add
,它只是不接受 name 和 surname 参数。所有值都保存到char **
数组中。
您能否建议我,如何处理这种情况,以便能够正确接受两种输入?
Ps:对于char*
输入,整个程序都可以正常工作,所以我不希望那里有任何错误.. Pps:请不要建议我使用字符串或我在这里不使用的任何其他结构 - 我的进口非常有限,因此我使用 char* 和其他东西......