2013-12-20 63 views
1

我想在C線後,搜索線在文件一行行,我使用這個代碼搜索通過在C

user = fopen("user.txt","r"); 
rewind(user); 
while(fgets(string_handler,250,user) != NULL) 
{ 
    sscanf(string_handler,"%s %s %c",e.user_e,e.pass_e,&e.account_e); 
    if(strcmp(user_name,e.user_e) == 0) 
    { 
     printf("\n\tUsername Already Exits choose another one!"); 
     break; 
    } 
    if(e.account_e == account_type) 
    { 
     printf("\n\tYou already have this kind of account!"); 
     break; 
    } 
    fclose(user); 
} 

所以我想要做的是,我想從這個文件中搜索記錄。但是這個代碼只搜索第一條記錄。我在文件中有3條記錄行,它只搜索第一條記錄。

+4

您正在循環結束時關閉文件。因此'fgets'失敗。 –

+1

我不認爲你需要倒帶一個新打開的文件指針,如果你用「r」打開 –

+0

即使我在最後關閉文件也不起作用!我只是用「a」打開同一個文件。如果記錄不匹配! – ahsanic

回答

4
  1. fclose(user)放在while(fgets())之後循環。
  2. 刪除break;找到記錄時。