2、关于EOF的两个小验证

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf("getchar() != EOF的对应值:%d\n", getchar()!=EOF);
    printf("EOF本身的值:%d\n", EOF);

    return 0;
}

  

 

转载于:https://www.cnblogs.com/Gozz/p/7764168.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: 好的,以下是使用链表写的C程序,包含登陆和注册模块,并且能够将账号信息写入文件: ``` #include <stdio.h> #include <stdlib.h> #include <string.h> struct User { char username[20]; char password[20]; struct User* next; }; struct User* createUser(char username[], char password[]) { struct User* newUser = (struct User*)malloc(sizeof(struct User)); strcpy(newUser->username, username); strcpy(newUser->password, password); newUser->next = NULL; return newUser; } void saveUsersToFile(struct User* head) { FILE* file = fopen("users.txt", "w"); struct User* current = head; while (current != NULL) { fprintf(file, "%s %s\n", current->username, current->password); current = current->next; } fclose(file); } struct User* loadUsersFromFile() { struct User* head = NULL; FILE* file = fopen("users.txt", "r"); if (file == NULL) { return NULL; } char username[20]; char password[20]; while (fscanf(file, "%s %s", username, password) == 2) { struct User* user = createUser(username, password); if (head == NULL) { head = user; } else { struct User* current = head; while (current->next != NULL) { current = current->next; } current->next = user; } } fclose(file); return head; } int main() { struct User* head = loadUsersFromFile(); while (1) { int choice; printf("请选择操作:\n"); printf("1. 注册\n"); printf("2. 登陆\n"); printf("3. 退出\n"); scanf("%d", &choice); if (choice == 1) { char username[20]; char password[20]; printf("请输入用户名:"); scanf("%s", username); printf("请输入密码:"); scanf("%s", password); struct User* user = createUser(username, password); if (head == NULL) { head = user; } else { struct User* current = head; while (current->next != NULL) { current = current->next; } current->next = user; } saveUsersToFile(head); printf("注册成功!\n"); } else if (choice == 2) { char username[20]; char password[20]; printf("请输入用户名:"); scanf("%s", username); printf("请输入密码:"); scanf("%s", password); struct User* current = head; int found = 0; while (current != NULL) { if (strcmp(current->username, username) == 0 && strcmp(current->password, password) == 0) { found = 1; break; } current = current->next; } if (found) { printf("登陆成功!\n"); } else { printf("用户名或密码错误!\n"); } } else if (choice == 3) { break; } else { printf("无效的选择!\n"); } } return 0; } ``` 这个程序使用了链表来存储用户信息,并且可以将用户信息写入文件以便 ### 回答2: 使用链表写一个C程序,实现登陆注册模块,需要验证账号信息是否存在且正确,并将数据写入文件中。 首先,我们需要定义一个链表节点的结构体,包含账号名和密码两个成员变量,还包括指向下一个节点的指针。 ```c typedef struct ListNode { char username[20]; char password[20]; struct ListNode* next; } ListNode; ``` 接下来,我们定义链表的头节点和其他相关变量。 ```c ListNode* head = NULL; // 头节点指针 ListNode* last = NULL; // 尾节点指针 FILE* fp; // 文件指针 ``` 然后,我们实现注册函数,将输入的账号名和密码加入到链表中。 ```c void registerUser(char* username, char* password) { ListNode* newNode = (ListNode*)malloc(sizeof(ListNode)); strcpy(newNode->username, username); strcpy(newNode->password, password); newNode->next = NULL; if (head == NULL) { head = newNode; last = newNode; } else { last->next = newNode; last = newNode; } // 将新节点写入文件 fp = fopen("user_data.txt", "a"); fprintf(fp, "%s %s\n", newNode->username, newNode->password); fclose(fp); } ``` 接着,我们实现登录函数,验证账号名和密码是否匹配。 ```c int loginUser(char* username, char* password) { ListNode* currentNode = head; while (currentNode != NULL) { if (strcmp(currentNode->username, username) == 0 && strcmp(currentNode->password, password) == 0) return 1; // 登录成功 currentNode = currentNode->next; } return 0; // 登录失败 } ``` 最后,我们可以在主函数中进行测试,并根据返回值判断登录是否成功。 ```c int main() { registerUser("Alice", "123"); registerUser("Bob", "456"); if (loginUser("Alice", "123")) printf("登录成功!\n"); else printf("登录失败!\n"); if (loginUser("Alice", "111")) printf("登录成功!\n"); else printf("登录失败!\n"); return 0; } ``` 这样,我们就可以使用链表实现一个简单的登陆注册模块,验证账号信息是否存在正确,并将数据写入文件。 ### 回答3: 下面是使用链表写一个C程序的登录注册模块的示例,实现了验证账号信息是否存在并且正确,并将数据写入文件。 ```C #include <stdio.h> #include <stdlib.h> #include <string.h> // 用户数据结构 typedef struct User { char username[20]; char password[20]; struct User* next; } User; // 保存用户数据到文件 void saveToFile(User* users) { FILE* file = fopen("users.txt", "w"); User* current = users; while (current != NULL) { fprintf(file, "%s|%s\n", current->username, current->password); current = current->next; } fclose(file); } // 从文件中加载用户数据 User* loadFromFile() { FILE* file = fopen("users.txt", "r"); User* users = NULL; User* tail = NULL; char username[20]; char password[20]; while (fscanf(file, "%[^|]|%s\n", username, password) != EOF) { User* newUser = (User*)malloc(sizeof(User)); strcpy(newUser->username, username); strcpy(newUser->password, password); newUser->next = NULL; if (users == NULL) { users = newUser; tail = newUser; } else { tail->next = newUser; tail = newUser; } } fclose(file); return users; } // 释放链表内存 void freeList(User* users) { User* current = users; while (current != NULL) { User* temp = current; current = current->next; free(temp); } } // 验证账号信息是否正确 User* authenticate(User* users, const char* username, const char* password) { User* current = users; while (current != NULL) { // 找到匹配的用户名和密码 if (strcmp(current->username, username) == 0 && strcmp(current->password, password) == 0) { return current; } current = current->next; } return NULL; } // 注册新用户 void registerUser(User** users, const char* username, const char* password) { User* newUser = (User*)malloc(sizeof(User)); strcpy(newUser->username, username); strcpy(newUser->password, password); newUser->next = *users; *users = newUser; saveToFile(*users); } int main() { User* users = loadFromFile(); // 登录验证示例 char username[20]; char password[20]; printf("请输入用户名:"); scanf("%s", username); printf("请输入密码:"); scanf("%s", password); User* loggedInUser = authenticate(users, username, password); if (loggedInUser != NULL) { printf("登录成功!\n"); } else { printf("用户名或密码错误!\n"); } // 注册示例 char newUsername[20]; char newPassword[20]; printf("请输入新用户名:"); scanf("%s", newUsername); printf("请输入新密码:"); scanf("%s", newPassword); registerUser(&users, newUsername, newPassword); // 释放内存并保存数据到文件 freeList(users); return 0; } ``` 在这个示例中,使用了一个链表来保存用户的账号信息,并通过文件来进行数据的读取和保存。具体实现了以下功能: 1. 从文件中加载用户数据到链表中; 2. 验证用户的账号信息是否正确,并返回对应的用户; 3. 注册新用户,将新用户的账号信息添加到链表中,并保存数据到文件; 4. 释放链表内存并保存数据到文件。 请注意,该示例为简化版,没有包含数据输入的验证、密码的加密和其他一些安全措施。在实际应用中,还需要对用户的输入进行合法性检查,并对敏感信息进行加密存储。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值