gpt4 book ai didi

c - printf 似乎正在覆盖自身

转载 作者:行者123 更新时间:2023-11-30 15:17:07 26 4
gpt4 key购买 nike

所以我正在尝试格式化这一行

******************* Flight Reservation Request  **********************
email name (M 1986/01/01)

但它打印的只是

******************* Flight Reservation Request  **********************
(M 1986/01/01)

执行此操作的代码是

FILE * pFileCust; // stream Input for Customer Reservation data
void processCommandSwitches(int argc, char * argv[], char * * ppszCustomerFileName);
void processReservations();
int main(int argc, char * argv[]) {
char * pszCustomerFileName = NULL;
// Process the command switches
processCommandSwitches(argc, argv, & pszCustomerFileName);
// open the Customer Reservation stream data file
if (pszCustomerFileName == NULL)
exitError(ERR_MISSING_SWITCH, "-c");
pFileCust = fopen(pszCustomerFileName, "r");
if (pFileCust == NULL)
exitError(ERR_CUSTOMER_RES_FILENAME, pszCustomerFileName);
// process the Reservations
processReservations();
fclose(pFileCust);
printf("\n"); // included so that you can put a breakpoint on this line
return 0;
}
/****** you need to document and code this function *****/
void processReservations() {
rewind(pFileCust);
char gender[1];
char dob[11];
char emailAddress[53];
char fullName[31];
char address[31];
char city[21];
char state[31];
char zip[61];
fscanf(pFileCust, "%s %s %s %31[^\n]", gender, dob, emailAddress, fullName);
printReservations(emailAddress, fullName, gender, dob, address, city, state, zip);
}
printReservations(char email[], char name[], char gender[], char dob[], char address[], char city[], char state[], char zip[]) {
printf("******************* Flight Reservation Request **********************\n");
printf("%s %s (%s %s)\n", email, name, gender, dob);
}

输入文件

M 1986/01/01 petem@xyz.net Pete Moss
123 Boggy Lane,New Orleans,LA,70112
H100.15005 2
H222.15005 2
H200.15010 2
H333.15010 2
END 0
M 1957/02/02 pcorn@abc.netPopCorn______________________
456 Kernel,San Antonio,TX,78210
H222.15005 10
HXXX.XXXXX 10
H333.15010 5
END 0
F 1958/03/03 pennyl@xyz.net Penny Loafer
789 Walking St,Philadelphia,PA,19102
H444.15001 1
H333.15010 1
END 0
M 1979/04/04 hgilmore@xyz.net Adam Sandler
444 Golf View,Hollywood,CA,92274
H100.15005 3
END 0
F 1989/05/05 butter@xyz.net Melba Toast
222 Cracker Blvd,San Antonio,TX,78222
H333.15010 2
H444.15015 2
END 0

最佳答案

基于退休忍者的建议:

You may have an embedded \r in the string. This can happen if you're reading a file created on Windows on an OS that doesn't use \r\n as a line end.

我更改了代码:

fscanf(pFileCust, "%s %s %s %31[^\n]", gender, dob, emailAddress, fullName);

对此:

fscanf(pFileCust, "%s %s %s %31[^\r\n]", gender, dob, emailAddress, fullName);

这解决了问题,我的输出现在看起来正常。

关于c - printf 似乎正在覆盖自身,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32675669/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com