c - 将 bin 文件中的字符串转换为 fgets() 字符串
问题描述
在我的代码中,我创建了 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 方面。
推荐阅读
- php - 使用时我的数据库查询不起作用?在声明中
- css - CSS - Internet Explorer 不会显示框阴影和过渡
- c - char ** 读取或 fclose 上的 C Seg 错误
- python - Python os.path.realpath 无法显示位置
- c# - 如何从数组的第一个或最后一个数字生成一个数字,并检查它是否可以被 C# 中的某个数字整除?
- reactjs - React:在功能组件中传递两个 onclick 函数
- .net - VB.Net 代码用冒号分组缩进:
- python - 输入框不会出现在 tkinter 窗口中
- python - 如何使用我自己的 Python 程序获取 Google Colab 单元格输出
- python - 使用 cmd 在 Windows 10 上安装 onnx 库失败