我遇到两个错误。我在网站上搜索过,发现转换类型字符和过多的参数错误来自sscanf
和printf
,这些错误发生在我的代码中的第166和186行。
代码处理从文本文件收集的信息位(如姓名,城市,州,DOB,电子邮件地址等)。我认为它与我的%
标志或括号格式[^\n]
有关。我看到另一个帖子,其中某些字符在括号内定义如下:%40[A-Za-z0-9._@] %[A-Za-z ]
但我不知道这是否是我需要做的才能解决至少未知的转换类型字符问题。任何帮助,将不胜感激。
变量在我的.h文件中声明,但名称前面的小写字母(c
,sz
和i
)表示char,char数组和整数。
以下是错误据称源自的代码的一部分。我注释掉了一个return语句,因为使用此代码的方法是void:
iScanfCnt = sscanf(szInputBuffer, "%1c %1s %4s %30[^\n] %30[^\n] %2s %2s %5s %s %d\n"
, &customer.cGender
, customer.szBirthDt
, customer.szEmailAddr
, customer.szFullName
, customer.szStreetAddress
, customer.szCity
, customer.szStateCd
, customer.szZipCd
, flight.szFlightId
, &flight.iRequestSeats);
// Check for bad input. scanf returns the number of valid conversions
if (iScanfCnt < 10)
{
printf("invalid input when reading student data, only %d valid values. \n"
, iScanfCnt);
printf("\tdata is %s\n", szInputBuffer);
//return ERR_MISSING_SWITCH;
}
printf("%1s %10.2s %10.2s %30[^n] %30[^n] %10.2s %2s %5s %s %d\n"
, &customer.cGender
, customer.szBirthDt
, customer.szEmailAddr
, customer.szFullName
, customer.szStreetAddress
, customer.szCity
, customer.szStateCd
, customer.szZipCd
, flight.szFlightId
, &flight.iRequestSeats);
}
我的错误:
p1xxxxxx.c: In function 'processReservations':
p1xxxxxx.c:166:4: warning: unknown conversion type character '0' in format [-Wformat=]
, &flight.iRequestSeats);
^
p1xxxxxx.c:166:4: warning: too many arguments for format [-Wformat-extra-args]
p1xxxxxx.c:186:5: warning: unknown conversion type character '[' in format [-Wformat=]
, &flight.iRequestSeats);
^
p1xxxxxx.c:186:5: warning: unknown conversion type character '[' in format [-Wformat=]
p1xxxxxx.c:186:5: warning: too many arguments for format [-Wformat-extra-args]
出了什么问题?
答案 0 :(得分:4)
以下是您的代码的工作示例
#include <stdio.h>
struct x
{
char cGender;
char szBirthDt[200];
char szEmailAddr[200];
char szFullName[200];
char szStreetAddress[200];
char szCity[200];
char szStateCd[200];
char szZipCd[200];
};
struct y
{
char szFlightId[200];
int iRequestSeats;
};
int main(void) {
struct x customer;
struct y flight;
int iScanfCnt;
char* szInputBuffer = "m a bbbb cccccc\nsome text\nde fg hijkl mnop 10";
iScanfCnt = sscanf(szInputBuffer, "%1c %1s %4s %30[^\n] %30[^\n] %2s %2s %5s %s %d\n"
, &customer.cGender
, customer.szBirthDt
, customer.szEmailAddr
, customer.szFullName
, customer.szStreetAddress
, customer.szCity
, customer.szStateCd
, customer.szZipCd
, flight.szFlightId
, &flight.iRequestSeats);
if (iScanfCnt < 10)
{
printf("invalid input when reading student data, only %d valid values. \n"
, iScanfCnt);
printf("\tdata is %s\n", szInputBuffer);
return -1;
}
// Fix: %c %s %s
printf("%1c %10.2s %10.2s %30s %30s %10.2s %2s %5s %s %d\n"
, customer.cGender // Fix: no &
, customer.szBirthDt
, customer.szEmailAddr
, customer.szFullName
, customer.szStreetAddress
, customer.szCity
, customer.szStateCd
, customer.szZipCd
, flight.szFlightId
, flight.iRequestSeats); // Fix: no &
return 0;
}
我只需修复printf
- sscanf
正常工作。
输出:
m a bb cccccc some text de fg hijkl mnop 10