C 将字符串从bin文件压缩到fgets()字符串
在我的代码中,我创建了3个新文件 如果文件“用户”不存在,我创建一个带有一些信息的文件来初始化文件 在此之后,我想使用fgets()函数比较来自用户的字符串 然后将字符串与文件中的字符串(密码和ID)进行比较 您可以在代码的开头看到struct:user。 现在,当我使用“strcmp”时,它总是给我“1”C 将字符串从bin文件压缩到fgets()字符串,c,string,file,strcmp,bin,C,String,File,Strcmp,Bin,在我的代码中,我创建了3个新文件 如果文件“用户”不存在,我创建一个带有一些信息的文件来初始化文件 在此之后,我想使用fgets()函数比较来自用户的字符串 然后将字符串与文件中的字符串(密码和ID)进行比较 您可以在代码的开头看到struct:user。 现在,当我使用“strcmp”时,它总是给我“1” enter code here #define CRT_SECURE_NO_WARNINGS #pragma warning(disable:4996) #include <stdi
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");
}
在此处输入代码
#定义CRT\u安全\u无\u警告
#杂注警告(禁用:4996)
#包括
#包括
#包括
#包括“doubly_list.h”
类型定义结构用户{
国际安全水平;
字符ID[15];
字符密码[15];
字符全名[20];
}用户;
typedef结构项{
char movie_name[20];
煤焦生产商名称[20];
int发布日期年、发布日期月、发布日期日;
浮动评级;
int序列号;
bool是成人视频;
}项目;
void main()
{
字符id_输入[15],密码_输入[15];
整数天、月、年、小时、分钟、安全级别;
文件*日志;
log=fopen(“log.txt”,“a”);
fclose(log);
文件*用户;
users=fopen(“users.dat”,“rb+”);
用户新用户;
如果(用户==NULL)
{
文件*用户;
users=fopen(“users.dat”,“ab+”);
new_user.SecurityLevel=3;
strcpy(&new_user.Password,“admin”);
strcpy(&new_user.ID,“admin”);
strcpy(&new_user.FullName,“系统管理器”);
fwrite(和new_用户),sizeof(new_用户),1,用户;
fclose(用户);
}
档案*项目;
item=fopen(“users.dat”,“ab+”);
fclose(项目);
printf(“输入ID:”);
fgets(id_输入,15,标准输入);
flushall();
printf(“输入密码:”);
fgets(密码输入,15,标准输入);
log=fopen(“log.txt”,“a+”);
fprintf(日志,“\n用户输入了ID:%s”,ID\u输入);
fprintf(日志,“用户输入密码:%s”,密码输入);
文件*users\u out=fopen(“users.dat”、“rb”);
fread(&new_user,sizeof(user),1,users_out);
int result=result=strcmp(密码输入,新用户密码);
printf(“\n%d\n”,结果);
if(strcmp(密码输入,新用户密码)==0)
printf(“\n登录成功…\n\n”);
其他的
printf(“\n登录失败…\n\n”);
fclose(log);
fclose(用户输出);
系统(“暂停”);
}
代码使用了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;
}
然后
关键的一点是,用户输入是有害的,最好读取helper函数中的一行输入,并让它处理I/O细微差别。让调用代码关注输入的非I/O方面
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
}