|
查看: 1133|回复: 8
|
怎样 compare data from file with the data key-in by user??
[复制链接]
|
|
|
本帖最后由 无知小子92 于 4-8-2011 05:47 PM 编辑
如题:
小弟现在在做着大学的assignment,问题是要 create 一个 library system
大致上都应该还 OK,但是我面对最大的问题是......如下
我要把 user key-in 的 id 与 saved file 里的 id compare... 如果不符合,show error(表示user log-in id not found)
可是,问题来了,我就是做不到这点
以下是小弟的 coding,请问谁可以帮忙看看哪里出了问题呢?
谢谢!
- #include<stdio.h>
- #include<stdlib.h>
- #include<string.h>
- int main (void)
- {
- int login;
- char lib_id[10], lib_pass[10], name[10], std_id[10], std_pass[10];
- FILE* lib_data;
- do
- {
- printf("Welcome to Library Systems\n\n");
- printf("Please choose your login\n");
- printf("\t1: Librarian\n");
- printf("\t2: Student\n\n");
- fflush(stdin);
- printf("Login as: ");
- scanf("%d", &login);
- if(login != 1 && login != 2)
- {
- printf("error\n\n");
- system("pause");
- system("cls");
- }
- }while(login != 1 && login != 2);
- if(login == 1)
- {
- system("cls");
- printf("\t\t\tYou are current login as librarian.\n\n");
- printf("Enter your ID: ");
- scanf("%s", &lib_id);
- printf("Enter your password: ");
- scanf("%s", &lib_pass);
- lib_data = fopen("Librarian_ID.txt", "r");
- if(!lib_data)
- {
- printf("Could not open input file\a\n");
- }
- if(strcmp(lib_id, fgets(name,10, lib_data)) == 0 && strcmp(lib_pass, fgets(name,10, lib_data)) == 0))
- {
- printf("What you want to do next??");
- printf("1.\tadd book\n");
- printf("2.\tcheck out book\n");
- }
- else
- {
- printf("error");
- }
- fclose(lib_data);
- }
- return 0;
- }
复制代码
从 line 49 到 line 58,不管我 key-in 的 ID 对与否,IF-ELSE selection 都起不了作用,什么问题呢?
谢谢! |
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2011 09:06 PM
|
显示全部楼层
- if(login != 1 && login != 2)
复制代码 因该用 || 吧
- if(strcmp(lib_id, fgets(name,10, lib_data)) == 0 && strcmp(lib_pass, fgets(name,10, lib_data)) == 0))
- {
- printf("What you want to do next??");
- printf("1.\tadd book\n");
- printf("2.\tcheck out book\n");
- }
- else
- {
- printf("error");
- }
复制代码 因该这里有问题 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 4-8-2011 10:16 PM
|
显示全部楼层
因该用 || 吧因该这里有问题
loonloon0625 发表于 4-8-2011 21:06  对啊~我也说明觉得是 从 line 49 到 line 58 有问题
请问你懂得如何解决吗?
谢谢 |
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2011 10:24 PM
|
显示全部楼层
很久没写C 了,
(strcmp(lib_id, fgets(name,10, lib_data))
(strcmp(ib_pass, fgets(name,10, lib_data))
你好像 lib_id 和 name,10 compare 完, 之后又用回name,10 跟 lib_pass compare
你试试retrive之后,printf 出来看是什么data  |
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2011 10:32 PM
|
显示全部楼层
本帖最后由 geekman 于 4-8-2011 10:35 PM 编辑
- if(strcmp(lib_id, fgets(name,10, lib_data)) == 0 && strcmp(lib_pass, fgets(name,10, lib_data)) == 0))
复制代码 Parentheses mismatch。'(' x5, ')' x6。。。
- int name_result, pass_result;
- 。。。
- fgets(name, 10, lib_data);
- name_result = strcmp(lib_id, name);
- fgets(name, 10, lib_data);
- pass_result = strcmp(lib_pass, name);
- if((name_result == 0) && (pass_result == 0))
- {
- ...
复制代码 看到你的错误在那里了吗?你的括弧的次序放错了,导致 == 和 && 的结果混乱而产生错误。别想着把几个步骤浓缩在一行里面,这不会让你的导师给你加分,也不会显得你很pandai,事实上大多数导师十分痛恨这种做法(我的亲身体验。。。被扣了10分呢)。将来如果你加入开发团体,你的这种恶习会让你被你的同事们打晕后架在BBQ炉上烤。。。
哦,对了,just a reminder, fgets读取的c string后面有个null terminator, so,你的name只能承载最大9个char的资料。如果你打算读取的是10个char。。。well, good luck。 |
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2011 10:39 PM
|
显示全部楼层
回复 5# geekman
是哦,我都没发觉到 |
|
|
|
|
|
|
|
|
|
|
发表于 4-8-2011 10:45 PM
|
显示全部楼层
回复 5# geekman
又上了一堂 |
|
|
|
|
|
|
|
|
|
|

楼主 |
发表于 5-8-2011 01:55 AM
|
显示全部楼层
|
|
|
|
|
|
|
|
|
|
发表于 5-8-2011 08:45 AM
|
显示全部楼层
本帖最后由 geekman 于 5-8-2011 09:06 AM 编辑
试试看把name[10]改成name[100], 然后把fgets()的result print出来, 看看是不是因为new line character 的处理方式不对造成读取的资料超过一行。直接检测读取和对比的资料是最直接的debug方法。甚至你不只是要print string,还得print ASCII code,以便检测是否是control character (line feed, new line)在搞鬼。用code break + add watch会比较方便啦。
另外,你上面的code显示的是你连续读取同样的file两次,第一次compare with lib_id,第二次compare with lib_pass。。。但是你的data全都是lib_id?
还有,你如何handle multiple user id check? User输入lib1, 可是你的data file里面有lib1\nlib2\nlib3,so, 你如何检测用户输入lib3的id是正确的?
最后,你用scanf来读取用户输入,scanf会ignore new line character(\n), 但是fgets却会保留\n。。。我觉得统一用scanf和fscanf会比较好。
以上论点纯为猜测,毕竟我已经有近10年没写Console program,stdio里的东西都几乎忘光了 加上手头上没有C compiler(现在改用C#了), 所以也无法验证你的coding,只能靠你自己去验证了。
p/s: 如果是我,我会用structure来保存用户id和密码,同时使用同样的structure来读取和写入资料:
- struct user_struct
- {
- char user_id[11]; //assume 10 char id
- char user_pass[11];
- };
- user_struct user_input, file_data;
- //user input:
- scanf("%s", user_input.user_id);
- scanf("%s", user_input.user_pass);
- ...
- //reading file:
- FILE *file = fopen("data.dat", "r"); //binary mode for reading
- fread(file_data, sizeof(user_struct), 1, file);
- ...
- //writing file:
- FILE *file = fopen("data.dat", "wb"); //binary write, or use "ab" for binary append, depend on your own usage
- fseek(file, 0l, SEEK_END); //go to end of file if you r not using append mode
- fwrite(user_input, sizeof(user_struct), 1, file);
- ...
- //compare:
- strcmp(user_input.user_id, file_data.user_id);
- strcmp(user_input.user_pass, file_data.user_pass);
复制代码 郑重声明 :以上代码不保证100%正确,毕竟这只是根据10年前的模糊记忆写出来的。 |
|
|
|
|
|
|
|
|
| |
本周最热论坛帖子
|