我问两个简单的用户输入,一个用户和一个密码,然后我将它们插入到一个文本文件中,每个文件后面都有一个分号。分号保存和密码保存,但用户名不能保存一些奇怪的原因。插入到文本文件空白
例如,如果我输入乔密码111222444555它会 显示为;111222444555;
代替Joe;111222444555;
代码:
int main()
{
int Number_Of_Attempts = 3;
int result = 0;
char userID[32];
printf("Please enter your user id\n");
scanf("%s", &userID);
char password[12];
printf("The user has not been found. Please enter your a password\n");
scanf("%s", &password);
printf("Username and Password has been saved");
printf("\n");
InsertIntoHash(userID, password);
return 0;
}
void InsertIntoHash(char *userID, char *hash)
{
FILE *fp;
fp = fopen("HashTable.txt", "a");
fprintf(fp, userID);
fprintf(fp,";");
fprintf(fp, hash);
fprintf(fp, ";\n");
fclose(fp);
}
为什么不'fprintf(fp,“%s;%s; \ n”,userId,hash);'? – John3136
可能出于某种原因,您正在写一些CR(回车)到文件中? “Joe; ...”的文件需要多长时间? – linuxfan
您应该打印两个变量,以便您可以看到提供给InsertIntoHash的内容。这将帮助您找出问题所在。 – hymie