c编程中的搜索函数



我正在尝试创建一个程序,该程序将列表保存到文本文件中,然后访问此列表以使用id搜索名称,然后显示该人员的名称,id和信息。

我的程序中有很多错误,但我不明白为什么搜索函数不搜索文本文件中的名称。

即使名称或id在文本文件中,搜索也总是显示"无法找到记录"。

请帮帮我。我想保存结构中的值,并从结构中使用它们。

这是我的代码。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>
struct VisitorData{
char *SrNo;
char *ticketNo;
char *Name;
char *ID;
};
struct ticket {
struct VisitorData visitor;
struct ticket *nextPtr;
}ticket1;
// ticket1* start = NULL;
int totalVisitors = 0;
char *my_itoa(int num, char *str)
{
if(str == NULL)
{
return NULL;
}
sprintf(str, "%d", num);
return str;
}
void addVisitor()
{
system("cls");
totalVisitors++;
int TotalVisitors = totalVisitors;
char fullName[30];
char ID[20];
// char serialNumber[2];
char yesOrNo[2];

FILE *fp = NULL;
printf("nYou have chosen to add a new visitor.");
// serialNumber[0] = TotalVisitors;
if ((fp = fopen("Tickets.txt", "r")) == NULL)
{
fp = fopen("Tickets.txt", "w");
fprintf(fp, "Total Visitors: n");
fclose(fp);
}
else
{
fp = fopen("Tickets.txt", "r+");
fseek(fp, 15, SEEK_SET);
fscanf(fp, "%d", &TotalVisitors);
rewind(fp);
fseek(fp, 15, SEEK_SET);
fprintf(fp, "%d", TotalVisitors);
fclose(fp);
}

fp = fopen("Tickets.txt", "a+");
printf("nEnter your Full name: ");
fflush(stdin);
// gets(fullName);
scanf(" %[^n]%*c", fullName);
char underScore = '_';
// replace space with underscore in a loop
for(int i = 0; i < strlen(fullName); i++){
if(fullName[i] == ' ')
fullName[i] = underScore;
}
printf("Your name: ");
puts(fullName);
fprintf(fp, "%d ", TotalVisitors);
printf("nAre you a Local? [Y/N]: ");
fflush(stdin);
scanf(" %s", &yesOrNo);
// gets(yesOrNo);

int fullNameLength = strlen(fullName);
char spaceToAdd = " ";
char ticketNoChar[20];
int characterRep[20];
// adding spaces if full name length is less than 6
if(fullNameLength < 6){
while(fullNameLength <6){
sprintf(fullName,"%s%s", spaceToAdd, fullName);
fullNameLength = strlen(fullName);
}
}
int ticketNo[20] = {}, i=0, j;
while(i!=5) {
ticketNo[i]=fullName[i]; i++;
}
printf("Your ticket number: ");
for(j=0;j<i-1;j++){
printf("%d",ticketNo[j]);
}
for (int i = 0; i < 6; i++)
{
fprintf(fp, "%d", (int)fullName[i]);
}
fprintf(fp, "%d ", TotalVisitors);
fprintf(fp, "%s ", fullName);
struct VisitorData v1;
if(strcmp(yesOrNo, "y") == 0){
printf("nPlease enter your local ID: ");
fflush(stdin);
scanf(" %s", &ID);
// gets(ID);
char localIDAdd[5] = "ID";
strcat(localIDAdd, ID);
printf("Your local ID: ");
puts(localIDAdd);
v1.ID = localIDAdd;
fprintf(fp, "%s n", localIDAdd);
} else if (strcmp(yesOrNo, "Y") == 0){
printf("nnPlease enter your local ID: ");
fflush(stdin);
scanf(" %s", &ID);
// gets(ID);
char localIDAdd[5] = "IC";
strcat(localIDAdd, ID);
printf("Your local ID: ");
puts(localIDAdd);
v1.ID = localIDAdd;
fprintf(fp, "%s n", localIDAdd);
}else {
printf("nn Please enter your passport number: ");
fflush(stdin);
scanf(" %s", &ID);
// gets(ID);
char localIDAdd[5] = "PASS";
strcat(localIDAdd, ID);
printf("Your passport number: ");
puts(localIDAdd);
v1.ID = localIDAdd;
fprintf(fp, "%s n", localIDAdd);
}

char serialNumber[40];
char ticketNum;
my_itoa(TotalVisitors, serialNumber);
//my_itoa(ticketNo, ticketNum);
// printf("ntotal visitors (char): %c", serialNumber);
v1.SrNo = serialNumber;
//v1.ticketNo = ticketNum;
//strcpy(v1.ticketNo, ticketNo);
v1.Name = fullName;
// v1.ID = ID;
printf("n Your ticket information is as follows: ");
printf("n Your serial number: ");
puts(v1.SrNo);
printf("n Your full name: ");
puts(v1.Name);
printf("n ID: ");
puts(v1.ID);
//
// printf("n Your ticket number: ");
// for(int l=0;l<i-1;l++){
//    printf("%d",v1.ticketNo[l]);
// }
// puts(v1.ticketNo);
printf("n %s %s %s %s", v1.SrNo, v1.ticketNo, v1.Name, v1.ID);
// ticket(v1);
//
printf("nVisitor's information has been updated successfully!n"
"nHow would you like to proceed?"
"n1) Take me back to the Menu."
"n2) Exit the program."
"n Enter choice: ");
int choice;
scanf("%d", &choice);
if (choice == 1){
main();
}
if (choice == 2){
printf("nThank You!n");
exit(0);
}

fclose(fp);
}
void LookupVisitor()
{
system("cls");
int found = 0;
int ID =0;
int SrNo = 0;
char Name[30] = "";
char fullName[30];
struct VisitorData v1;
FILE *fp = NULL;
fp = fopen("Tickets.txt","r");
if(fp == NULL)
{
printf("ntttFile is not openedn");
exit(1);
}
printf("You have chosen to search for a visitor.");
if (fseek(fp, 15, SEEK_SET) != 0)
{
fclose(fp);
printf("nFacing Issue");
exit(1);
}
printf("nPlease select a way to search: ");
printf("n1) ID.");
printf("n2) Name.");
printf("n3) Ticket number.");
printf("n4) Go back to main menu.");
int choose;
printf("n Enter your choice = ");
fflush(stdin);
scanf("%d", &choose);
printf("nn");
if (choose == 1)
{
printf("nntttEnter Visitor ID to search:");
fflush(stdin);
scanf("%u",&ID);
while (fread (&v1, sizeof(v1), 1, fp))
{
if(v1.ID == ID)
{
found = 1;
break;
}
}
if(found)
{
printf("ntttPassenger id = %dn",v1.ID);
printf("ntttPassenger name = %sn",v1.Name);
//printf("ntttPassenger Address = %sn",v1.ticketNo);
}
else
{
printf("nThere is no Record. Please Try again.");
}
}
fclose(fp);
}
// int Name;
// if (choose == 2){
// printf("nntttEnter Visitor name to search:");
// fflush(stdin);
// scanf("%s",&Name);
// while (fread (&AddVisitor, sizeof(AddVisitor), 1, fp))
// {
//     if(AddVisitor.Name == Name)
//     {
//         found = 1;
//         break;
//     }
// }
// if(found)
// {
//     printf("ntttPassenger id = %dn",AddVisitor.ID);
//     printf("ntttPassenger name = %sn",AddVisitor.Name);
//     printf("ntttPassenger Address = %sn",AddVisitor.ticketNo);
// }
// else
// {
//     printf("nThere is no Record. Please Try again.");
//     fflush(stdin);
// }
// }
// int ticketNo;
// if (choose == 3){
// printf("nntttEnter Visitor's Ticket Number to search:");
// fflush(stdin);
// scanf("%u",&ticketNo);
// while (fread (&AddVisitor, sizeof(AddVisitor), 1, fp))
// {
//     if(AddVisitor.ticketNo == ticketNo)
//     {
//         found = 1;
//         break;
//     }
// }
// if(found)
// {
//     printf("nPassenger id = %dn",AddVisitor.ID);
//     printf("nPassenger name = %sn",AddVisitor.Name);
//     printf("nPassenger Address = %sn",AddVisitor.ticketNo);
// }
// else
// {
//     printf("nThere is no Record. Please Try again.");
//     fflush(stdin);
// }
// }
// else if(choose == 4){
// main();
// }
// else
// {
//     printf("nInvalid Input Please try again.");
// }
//}
void DeleteVisitor(){
}
void DisplayTickets(){
system("cls");
/* File pointer to hold reference to our file */
FILE * fPtr;
char ch;

/*
* Open file in r (read) mode.
* "data/file1.txt" is complete file path to read
*/
fPtr = fopen("tickets.txt", "r");

/* fopen() return NULL if last operation was unsuccessful */
if(fPtr == NULL)
{
/* Unable to open file hence exit */
printf("Unable to open file. File does't exist.n");
exit(EXIT_FAILURE);
}

/* File open success message */
printf("Reading file contents... nn");
do
{
/* Read single character from file */
ch = fgetc(fPtr);
/* Print character read on console */
putchar(ch);
} while(ch != EOF); /* Repeat this if last read character is not EOF */
printf("nAbove is the list of Visitors that are ticketholders.n"
"nHow would you like to proceed?"
"n1) Take me back to the Menu."
"n2) Exit the program."
"n Enter choice: ");
int Next;
scanf("%d", &Next);
if (Next == 1){
main();
}
if (Next == 2){
printf("nThank You!n");
exit(0);
}
/* Done with this file, close file to release resource */
fclose(fPtr);
}

int main(void){
system("cls");
printf("%s", "Hello. Welcome to Farah's theme park! Please choose an option from 1-4 to select what you'd like to do.");
printf("n1) Add a new visitor's information.");
printf("n2) Look up a visitor's information.");
printf("n3) Delete a visitor's information.");
printf("n4) Display the total number of tickets sold.");
printf("n0) Exit");
printf("n Enter your choice = ");
int choice = 0;
scanf("%d", &choice);
switch(choice)
{
case 1:
addVisitor();
break;
case 2:
LookupVisitor();
break;
case 3:
DeleteVisitor();
break;
case 4:
DisplayTickets();
break;
case 0:
printf("nThank you!n");
exit(1);
break;
default:
printf("nnInvalid Input. Please try again.");
}
//Switch Ended
while(choice!=0);
}
struct VisitorData{
char *SrNo;
char *ticketNo;
char *Name;
char *ID;
};

好的,一个VisitorData包含四个指针。

void LookupVisitor()
{
// ...
struct VisitorData v1;
FILE *fp = NULL;
fp = fopen("Tickets.txt","r");
// ...
printf("You have chosen to search for a visitor.");
// ...
if (choose == 1)
{
printf("nntttEnter Visitor ID to search:");
fflush(stdin);
scanf("%u",&ID);
while (fread (&v1, sizeof(v1), 1, fp))
{
if(v1.ID == ID)
{
found = 1;
break;
}

所以这没有意义。对于每个条目,从文件中读取四个指针(因为v1是一个VisitorData,而VisitorData是四个指针)。然后检查指针的值是否等于输入的ID。

但是为什么会发生呢?看看当您将v1.ID写入文件时:

char localIDAdd[5] = "ID";
// ...
v1.ID = localIDAdd;

因此,当你将v1.ID写入文件时,它包含了一个字符数组的地址,该字符数组是该函数的本地地址,但不再存在。因此,比较检查用户为ID输入的值是否等于一个字符数组的地址,该字符数组在您将ID写入文件时存在,但已不存在。

你需要重新考虑你写入文件的数据,以便它是你想要存储在文件中的实际数据,而不是指向即将释放的内存块的指针,其值没有进一步的意义。

相关内容

  • 没有找到相关文章

最新更新