I am creating a code that reads input constantly from the keyboard and compare it to a text file that contains usernames. each line in the text file contains only one name. The program detects if the text input matches the usernames in the line.
I know how to do it by looping. but I dont want to make the loop go through the whole file each time. I want the code to exclude the cases when it already detect input from the first time.
for example if the first time input is Ben
and the text file is
Ben
Tom
Albert
Alex
I want the program to loop in the first time and detect for matching text.
but in the second time if the input is different eg. Alex
then the program only start looping from the second line and exclude the first line from looping.
Here is some of my code in the main function
FILE * fp;
char Line[150];
char user[150];
printf("enter name: ");
fgets(user,150, stdin);
fp = fopen ("users.txt", "r");
if(fp == NULL)
{
perror("Error.. ");
exit(EXIT_FAILURE);
}
int line_start;
while(!feof(fp))
{
fgets(Line, 150, fp);
if (feof(fp))
break;
int store_start[150];
long store_end[150];
int i=0;
if (strcmp(user, Line) == 0)
{
//printf("%s\n", "match found");
fseek(fp, 0, SEEK_CUR);
long line_end = ftell(fp);
line_start = line_end - strlen(Line);
store_start[i]=line_start;
store_end[i]=line_end;
printf("%s\n", "match found")
for (int i = 0; i < 150; ++i)
{
if (line_start==store_start[i] && line_end==store_end[i])
{
//skipp reading
fseek(fp, 1, SEEK_CUR);
printf("%ld\n",ftell(fp) );
}
}
printf("start is %d end is %ld \n", store_start[i],store_end[i]);
}
i++;
}
My approach is to detect the first occurrence of the word and if match happens. store the index of the starting of the line into an array store_start[150]
and ending of the line into another array store_end[150]
for which the matching happened and next time read every thing before the starting index and after the ending sentence of the line.
The problem is I don't know what to put inside this loop This loops basically checks if the loop gets to the same line which it read before in any of the previous loops.
if (line_start==store_start[i] && line_end==store_end[i])
{
//skipp reading
fseek(fp, 1, SEEK_CUR);
printf("%ld\n",ftell(fp) );
}
I want the code to basically skip reading this line and jump to the next line.