2017-04-22 123 views
-3

我正试图使简单的登录系统,基于txt。 的file看起来像C编程登录系统

1 1 
2 2 
3 3 

但我的代码检查txt只是最后一行,而不是每一行。

int write_login_and_pass() { 
    char login[30], pass[30]; 
    int select3; 

    printf("\n|---------Login:"); 
    scanf("%s", login); 
    printf("|---------Password:"); 
    scanf("%s", pass); 

    sign_In(login, pass); 
    _getch(); 
    system("PAUSE"); 
    return 0; 
} 
int sign_In(char login[30], char pass[30]) { 
    FILE *file; 
    char user2[30], pass2[30], fc; 
    file = fopen("Customers.txt", "r"); 
    char arra[128][128]; 

    if (file != NULL) { 
     char line[128]; 
     do { 
      fscanf(file, "%29s %29s", user2, pass2); 
     }while (fgets(line, sizeof line, file) != NULL); 
       if ((strcmp(login, user2) == 0) && (strcmp(pass, pass2) == 0)) { 
        printf("\n>>>User and password correct!<<<\n"); 
        fc = main_menu(); 
       } 
       else { 
        printf("\n>>>User or password incorrect!<<<\n"); 
        system("PAUSE"); 
        fc = sign_In(login, pass); 
       } 
    } 
    else printf("File was not founded"); 
    fclose(file); 
    system("PAUSE"); 
    return 0; 
} 
+0

请仔细阅读每个被调用函数的文档(如[fscanf](http://en.cppreference.com/w/c/io/fscanf),whos应检查返回值)。编译所有警告和调试信息(例如,如果使用[GCC](http://gcc.gnu.org/)...),则使用'gcc -Wall -g'。改进你的代码,以获得没有警告。 **使用调试器**'gdb'。 –

+1

仔细看看你的循环读取文件。如果你不能直接看到它,请花点时间[阅读如何调试小程序](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/)并学习如何使用调试器逐行执行代码。 –

+1

您的* fix-my-code *问题是无关紧要的。 –

回答

1

问题是在这里:

do 
{ 
     fscanf(file, "%29s %29s", user2, pass2); // 1) 
} 
while (fgets(line, sizeof line, file) != NULL); // 2) 

首先,在对用户密码1的读取),那么你尝试读第2行)的结束 - 但如果成功,作为一个while循环,你现在已经评估了密码读取至今之前重新进入......

两个选择,第一,简单的一个:

char user2[30], pass2[30], fc; 
file = fopen("Customers.txt", "r"); 
if (file) 
{ 
    while(fscanf(file, "%29s %29s", user2, pass2) == 2) 
    { 
     // do comparisons here 
    } 
    fclose(file); 
} 

请注意,您的绝对是依靠正确的文件格式,即i。即缺少对一个一次性

u1 p1 
u2 
p2 u3 
p3 u4 p4 

但是,您的登录:始终遵循一个用户,然后他/她的相关密码–空白不要紧,顺便说一下,这样文件甚至可以是这样的完全是坏了!

第二个选项:逐行读取行:

char user2[30], pass2[30], line[128]; 
file = fopen("Customers.txt", "r"); 
if (file) 
{ 
    while(fgets(line, sizeof line, file) 
    { 
     if(sscanf(line, "%29s %29s", user2, pass2) == 2) 
     //^(!) 
     { 
      // do comparisons here 
     } 
     // else: line is invalid! 
    } 
    fclose(file); 
} 

我个人宁愿strtok代替sscanf的,不过,因为你不必这样复制的字符串:

char *user2, *pass2, line[128]; 
file = fopen("Customers.txt", "r"); 
if (file) 
{ 
    while(fgets(line, sizeof line, file) 
    { 
     if((user2 = strtok(line, " \t") && (pass2 = strtok(NULL, " \t")) 
     { 
      // do comparisons here 
     } 
     // else: line is invalid! 
    } 
    fclose(file); 
} 

(只有缺点:strtok不是thread safe