c - 如何比较C程序文件中的字符串结构数组
问题描述
我正在为帐户交易编写一个项目。但是,在我的支票账户 ID 过程中存在一些小问题。我使用 struct 函数属于字符串数组,它可以帮助我稍后轻松解决我的子任务,重点是我尝试使用字符串比较检查 account_ID。输出无法显示我的期望。我不知道如何扫描数组中的变量以检查文件文本中的字符串数组结构。
我努力通过多种方式扫描变量。但它不起作用。这是我的代码。
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!”
* Expected Result:
Print "Account ID has been already used ! Please Enter other ID !"
解决方案
也许尝试使用 strstr 以防 id 总是相同的大小,这样它就会
if (strstr(customer[i].id,customer[a].id))
这样你的持久代码空间
推荐阅读
- python - Numpy 滚动 Columwise 相关性
- sql - SQL选择小于特定日期的最大日期
- python - sklearn.Normalization 是否有可用的 invert_transform 方法
- html - 用于更改图像的媒体查询
- hibernate - 不获取在休眠中插入的最近记录
- javascript - 在 TypeScript 中使用 subscribe 方法操作值
- c# - 为什么从 VS 2013 而不是 VS 2017 发布时,带有嵌入式 Adobe Reader Control 的应用程序可以工作?
- c++ - 未创建日志文件
- python - Django 中的实时搜索
- c# - 来自其他列表的新列表