[英]compering string from bin file to fgets() string
在我的代碼中,我創建了 3 個新文件。
如果文件“用戶”不存在,我會創建一個包含一些信息來初始化文件的文件。
之后,我想使用 fgets() 函數比較來自用戶的字符串,然后將字符串與文件中的字符串(密碼和 ID)進行比較,您可以在代碼的開頭看到 struct: user。 現在,當我使用“strcmp”時,它總是給我“1”。
enter code here
#define CRT_SECURE_NO_WARNINGS
#pragma warning(disable:4996)
#include <stdio.h>
#include <stdbool.h>
#include <time.h>
#include "doubly_list.h"
typedef struct user {
int SecurityLevel;
char ID[15];
char Password[15];
char FullName[20];
}user;
typedef struct items {
char movie_name[20];
char producer_name[20];
int release_date_year, release_date_month, release_date_day;
float rating;
int serial_number;
bool is_adult_video;
}items;
void main()
{
char id_input[15], password_input[15];
int day, month, year, hours, mins, SecurityLevel;
FILE* log;
log = fopen("log.txt", "a");
fclose(log);
FILE* users;
users = fopen("users.dat", "rb+");
user new_user;
if (users == NULL)
{
FILE* users;
users = fopen("users.dat", "ab+");
new_user.SecurityLevel = 3;
strcpy(&new_user.Password, "admin");
strcpy(&new_user.ID, "admin");
strcpy(&new_user.FullName, "System_Manager");
fwrite(&new_user, sizeof(new_user), 1, users);
fclose(users);
}
FILE* item;
item = fopen("users.dat", "ab+");
fclose(item);
printf("Enter ID: ");
fgets(id_input, 15, stdin);
flushall();
printf("Enter Password : ");
fgets(password_input, 15, stdin);
log = fopen("log.txt", "a+");
fprintf(log, "\nthe user entered the ID : %s", id_input);
fprintf(log, "the user entered the password : %s", password_input);
FILE* users_out= fopen("users.dat", "rb");
fread(&new_user, sizeof(user), 1, users_out);
int result= result = strcmp(password_input, new_user.Password);
printf("\n%d\n", result);
if (strcmp(password_input, new_user.Password)==0)
printf("\nLog in was successful.....\n\n");
else
printf("\nLog In was Unseccessful.....\n\n");
fclose(log);
fclose(users_out);
system("pause");
}
代碼使用fgets()
並且無法刪除尾隨'\\n'
。 然后輸入緩沖區至少 1 太小
printf("Enter ID: ");
fgets(id_input, 15, stdin); // likely has \n in `id_input`.
而是考慮一個輔助函數,例如
// read a line of input
// 1: Success
// 0: bad input
// EOF: end-of-file or rare input error
int read_line80(const char *prompt, char *dest, size_t sz) {
char buf[80 * 2]; // Be generous to allow for casual over long input
// If VLAs allowed, consider `char buf[sz + 2];`
if (prompt) fputs(prompt, stdout);
fflush(stdout); // Make certain output seen before input
if (sz > 0) dest[0] = '\0';
if (fgets(buf, sizeof buf, stdin) == NULL) return EOF;
size_t len = strlen(buf);
if (len > 0 && buf[len-1] == '\n') {
buf[--len] = '\0';
}
// Maybe add extra code here to see if `fgets()` did not read everything
if (len >= sz) return 0;
strcpy(dest, buf);
return 1;
}
然后
if (read_line("Enter ID: ", id_input, sizeof id_input == 1) &&
read_line("Enter Password : ", password_input, sizeof password_input == 1)) {
// Oh happy day!
// Use input
} else {
// Fail
}
關鍵是用戶輸入是邪惡的,最好在輔助函數中讀取一行輸入並讓它處理 I/O 細微差別。 讓調用代碼專注於輸入的非 I/O 方面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.