如何比较C程序文件中的字符串结构数组
我正在为帐户事务编写一个项目。然而,在我的支票帐户ID过程中有一些小问题。我使用了struct函数allen string array,这可以帮助我以后轻松地解决我的子任务,关键是我尝试使用字符串比较检查帐户ID。输出无法显示我的期望值。我不知道如何扫描数组中的变量以检查文件文本中的字符串数组结构 我尝试了很多方法来扫描一个变量。但它不起作用。这是我的密码如何比较C程序文件中的字符串结构数组,c,C,我正在为帐户事务编写一个项目。然而,在我的支票帐户ID过程中有一些小问题。我使用了struct函数allen string array,这可以帮助我以后轻松地解决我的子任务,关键是我尝试使用字符串比较检查帐户ID。输出无法显示我的期望值。我不知道如何扫描数组中的变量以检查文件文本中的字符串数组结构 我尝试了很多方法来扫描一个变量。但它不起作用。这是我的密码 struct customers{ char phone[13]; char id[9]; char name[31]; char addr
struct customers{
char phone[13];
char id[9];
char name[31];
char address[201];
char city[31];
char date[11];}customer
int main()
{
FIle *fc
if((fc=fopen("clients.txt","a+"))=NULL)
printf("Can not open");
else
{
int i = 0, a = 0;
fflush(stdin);
customer_ID:
printf("Enter the ID (maximum 8 digits): ";
gets(customer[i].id);
if (strlen(customer[i].id)!=8)
{
printf("Wrong the number of digits ! Enter Again!\n");
goto customer_ID;
}
while(fscanf(fc,"%s\n%[^\n]%*c%s\n%[^\n]%*c%[^\n]%*c%s\n\n",customer[a].id,customer[a].name,customer[a].phone,customer[a].address,customer[a].city,customer[a].date)!=EOF)
{
if (strcmp(customer[i].id,customer[a].id)==0)
{
printf("Account ID has been already used ! Please Enter other ID !\n");
goto customer_ID;
}
}
输出:
*实际结果:
它不打印“帐户ID已被使用!请输入其他ID!”
也许可以尝试使用strstr,以防id始终处于相同的大小,以便
if (strstr(customer[i].id,customer[a].id))
这样,您的持久代码空间谁或什么文本建议使用
fflush(stdin)代码>代码不编译,考虑重新发布真正的可编译代码。<代码> < <代码> >代码> < < /代码>均为0。比较同一字符串的结果是什么?旁注:(fc=fopen(“clients.txt”,“a+”))=NULL
将始终为false,并且还有将fc
设置为NULL的副作用。还请注意,fscanf
可能会失败并返回除EOF
以外的值。最好将返回值与要读取的预期项数进行比较。或者更好的方法是,不要使用fscanf
,因为它很容易使缓冲区溢出。
if (strstr(customer[i].id,customer[a].id))