我正在尝试用 C 创建一个具有唯一登录名的“银行”程序,为此,我有一个用户名文件,并检查用户输入的用户名是否与预先存在的用户名相匹配,并在下面进行了操作
FILE *pF = fopen("usernames.txt", "a+"); //opens 2nd file to read it, and creates it if it doesn't exist
//int length = sizeof(userName) / sizeof(userName[0]);
while (fgets(buffer, 255, pF)) { //reads file adn checks if the username is on it
if (strncmp(buffer, userName, strlen(userName)) == 0) { //checks the amount of characters in the string userName and only compares those characters
uniqueUsername = false;
break;
}
}
但是我的问题是,如果存在预先存在的用户名(例如“Daniel”),则无法创建“Dan”作为另一个用户名。
我尝试将
strlen(userName)
更改为 strlen(buffer)
,但这会比较缓冲区字符串的所有 255 个字符(我将其创建为 255 字节的虚拟数组)并导致不匹配。有什么办法可以解决这个问题吗?
在 C 中,您可以使用