2017-04-22 1 views
-3

txt을 기반으로 간단한 로그인 시스템을 만들려고합니다. fileC 프로그래밍 로그인 시스템

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)와 같은 모든 호출 된 함수의 설명서를 자세히 읽으십시오. e 반환 값을 확인해야 함). 모든 경고 및 디버그 정보로 컴파일하십시오 (예 : [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) 

첫째, 당신은 다음 라인 2)의 끝을 읽을을 시도 ) 사용자 암호 (1)의 한 쌍 읽기 - 성공을하지만, 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); 
} 

은 올바른 파일 형식을 사용한다는 점에 유의하십시오. 이자형. 항상 그런데 – 공백이 문제가되지 않습니다 그/그녀의 관련 암호 다음에 하나의 사용자를, 다음, 그래서 파일도 같이 볼 수 있었다 그러나

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); 
} 

나는 개인적으로,하지만, 대신 sscanf에서의 strtok 선호 : 라인으로 읽는 라인

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이 아닙니다.