如何通过if语句获取用户信息?
How to get access to user information by if statement?
我试图通过询问用户的姓名然后向他显示详细信息来授予用户访问其信息的权限。那我该怎么做呢?每当我命名时,它都不给我访问权限,这意味着我无权访问。我尝试并搜索了很多,但没有找到正确的答案。
int list_view(char *name, size_t namesize){
printf("For person details, please enter the person name and id card number: \n");
printf("Enter your Name: ");
if(!fgets(name, namesize, stdin)){
printf("No access");
return 0;
}
FILE * fr;
int one_by_one;
fr = fopen("/home/bilal/Documents/file.txt", "r");
for (int i=0; i<8; i++){
printf("\nHere is your ");
while((one_by_one = fgetc(fr)) != EOF && one_by_one != '\n'){
printf("%c",one_by_one); /* display on screen*/
}
}
fclose(fr);
return 0;
}
如果你看我之前对同一个问题的回答,我说你必须写
if(!fgets(dob, dobsize, stdin))
看到“!” if
中的字符
假设您的文件在同一行中包含名称和这些名称的详细信息,在文件中按名称创建简单搜索并将其与输入的名称进行比较。假设行号不超过 199 个字符,不包括不超过 99 个字符的名称和一个单词,下面是一个可能的实现示例:
int list_view(char *name, size_t namesize)
{
char line[200];
char name2[100];
printf("For person details, please enter the person name and id card number: \n");
printf("Enter your Name: ");
if (!fgets(name, namesize, stdin)){//read name from stdin
perror("Error while reading!");
return 0;
}
name[strcspn(name, "\n")] = '[=10=]'; //remove \n
FILE *fr;
if(!(fr = fopen("/home/bilal/Documents/file.txt", "r"))){ //always check fopen for errors
perror("Unable to open file");
return 0;
}
while(fscanf(fr, "%99s", name2) == 1) //search name in the file
{
if(!strcmp(name2, name)){ //the name is found print the rest of the line
if(fscanf(fr, "%199[^\n]", line) == 1){
printf("%s", line);
fclose(fr);
return 1;
}
}
}
fclose(fr);
return 0;
}
我试图通过询问用户的姓名然后向他显示详细信息来授予用户访问其信息的权限。那我该怎么做呢?每当我命名时,它都不给我访问权限,这意味着我无权访问。我尝试并搜索了很多,但没有找到正确的答案。
int list_view(char *name, size_t namesize){
printf("For person details, please enter the person name and id card number: \n");
printf("Enter your Name: ");
if(!fgets(name, namesize, stdin)){
printf("No access");
return 0;
}
FILE * fr;
int one_by_one;
fr = fopen("/home/bilal/Documents/file.txt", "r");
for (int i=0; i<8; i++){
printf("\nHere is your ");
while((one_by_one = fgetc(fr)) != EOF && one_by_one != '\n'){
printf("%c",one_by_one); /* display on screen*/
}
}
fclose(fr);
return 0;
}
如果你看我之前对同一个问题的回答,我说你必须写
if(!fgets(dob, dobsize, stdin))
看到“!” if
中的字符假设您的文件在同一行中包含名称和这些名称的详细信息,在文件中按名称创建简单搜索并将其与输入的名称进行比较。假设行号不超过 199 个字符,不包括不超过 99 个字符的名称和一个单词,下面是一个可能的实现示例:
int list_view(char *name, size_t namesize)
{
char line[200];
char name2[100];
printf("For person details, please enter the person name and id card number: \n");
printf("Enter your Name: ");
if (!fgets(name, namesize, stdin)){//read name from stdin
perror("Error while reading!");
return 0;
}
name[strcspn(name, "\n")] = '[=10=]'; //remove \n
FILE *fr;
if(!(fr = fopen("/home/bilal/Documents/file.txt", "r"))){ //always check fopen for errors
perror("Unable to open file");
return 0;
}
while(fscanf(fr, "%99s", name2) == 1) //search name in the file
{
if(!strcmp(name2, name)){ //the name is found print the rest of the line
if(fscanf(fr, "%199[^\n]", line) == 1){
printf("%s", line);
fclose(fr);
return 1;
}
}
}
fclose(fr);
return 0;
}