我想從用戶收集數據,我想用fgets()
完成任務。避免`fgets()`double輸入匹配
在我main.c
文件:
do
{
user = ask_user_info();
// ... Code exporting data to file ...
free(user);
fprintf(stdout, "Do you want to add another user?\nChoice: ");
scanf("%c[^\n]", &choice);
} while (choice == 'y');
下面是我寫的,把工作完成的功能:
UserData *ask_user_info()
{
char firstname[STRLEN];
char lastname[STRLEN];
char username[STRLEN];
char password[STRLEN];
char email[STRLEN];
fprintf(stdout, "First Name: ");
get_user_input(firstname);
flush_stdin();
fprintf(stdout, "Last Name: ");
get_user_input(lastname);
flush_stdin();
fprintf(stdout, "Username: ");
get_user_input(username);
flush_stdin();
fprintf(stdout, "Password: ");
get_user_input(password);
flush_stdin();
fprintf(stdout, "Email: ");
get_user_input(email);
flush_stdin();
return fill_fields(firstname, lastname, username, password, email);
}
void get_user_input(char *input)
{
int length;
char *buffer = (char *) malloc (STRLEN * sizeof(char));
(*buffer) = '\0';
if (fgets(buffer, STRLEN, stdin) != NULL)
{
length = strlen(buffer)-1;
buffer[length] = '\0';
strncpy(input, buffer, length+1);
}
free(buffer);
}
UserData *fill_fields(const char firstname[], const char lastname[],
const char username[], const char password[], const char email[])
{
UserData *user = (UserData *) malloc (sizeof(UserData));
user->firstname = (char *) malloc (strlen(firstname) * sizeof(char));
strncpy(user->firstname, firstname, strlen(firstname));
user->lastname = (char *) malloc (strlen(lastname) * sizeof(char));
strncpy(user->lastname, lastname, strlen(lastname));
user->username = (char *) malloc (strlen(username) * sizeof(char));
strncpy(user->username, username, strlen(username));
user->password = (char *) malloc (strlen(password) * sizeof(char));
strncpy(user->password, password, strlen(password));
user->email = (char *) malloc (strlen(email) * sizeof(char));
strncpy(user->email, email, strlen(email));
return user;
}
void flush_stdin()
{
int c;
while ((c = getchar()) != '\n' && c != EOF);
}
一切編譯,一切似乎正常工作,但該方案要求時對於用戶輸入(大部分時間),它要求用戶點擊輸入兩次以移動到下一個輸入。
一方面我知道這種行爲是由flush_stdin()
函數引起的。另一方面,我無法擺脫flush_stdin()
函數,因爲它確保沒有輸入字段被跳過。如果我做的程序會輸出類似於First Name: Last Name:
如何避免雙擊並確保收集所有輸入?
我認爲所有'malloc()'調用都是不必要的。簡單地讀入緩衝區,將它們的長度和它們的指針一起傳遞。 – trojanfoe