【C语言程序设计第四版】例12-4代码
#include <stdio.h> #include <stdlib.h> #include <string.h> struct sysuser{ char username[20]; char password[8]; }; void encrypt_(char *pwd); int checkUserValid(struct sysuser *psu); int main(void){ struct sysuser su; printf("Enter username: ");scanf("%s",su.username); printf("Enter password: ");scanf("%s",su.password); if (checkUserValid(&su)==1) { printf("Valid user!\n"); }else{ printf("Invalid user!\n"); } return 0; } void encrypt_(char *pwd){ int i; for (i = 0; i<strlen(pwd); i++) { pwd[i] = pwd[i]^15; } } int checkUserValid(struct sysuser *psu){ FILE *fp; char usr[30], usr1[30], pwd[10]; int check=0; strcpy(usr, psu->username); strcpy(pwd, psu->password); encrypt_(pwd); strcat(usr, " ");strcat(usr, pwd);strcat(usr, "\n"); if ((fp = fopen("f12-2.txt", "r"))==NULL) { printf("File open error!\n"); exit(0); } while (!feof(fp)) { fgets(usr1, 30, fp); // 全文扫描,牛逼 if (strcmp(usr, usr1) == 0) { check =1;break; } } if (fclose(fp)) { printf("Can not close the file!\n"); exit(0); } return check; }