0

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.

John C.
  • 405
  • 5
  • 19
  • `while(!feof(fp)) {` <<-- who taught you this? – wildplasser Oct 27 '19 at 16:40
  • Why? what's the problem? this is to loop till the end of the file is reached. – John C. Oct 27 '19 at 16:44
  • 2
    [Why is “while ( !feof (file) )” always wrong?](https://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong). Actually, you do it ok here, as you later do `if (feof(fp)) break`. What you really meant is `while (fgets(...))`. – KamilCuk Oct 27 '19 at 16:45
  • https://stackoverflow.com/q/5431941/905902 No, it does not. It loops one more time. (you can only detect that your head hit the wall *after* hitting the wall.) – wildplasser Oct 27 '19 at 16:46
  • @wildplasser you are true if the code doesn't contain that part `if (feof(fp)) break` other than this it breaks correctly – John C. Oct 27 '19 at 16:53
  • With these short lines of different lengths, it is probably easier to read the line anyway and just ignore it instead of fiddling with file positions and `fseek`. But why do you read from the file each time and don't store the data in memory? – M Oehm Oct 27 '19 at 17:03
  • @KamilCuk no it is not OK either. `if (feof(fp))` does not make it right. – Antti Haapala -- Слава Україні Oct 27 '19 at 17:05
  • @MOehm I dont want the loop to read lines that are known to be from previous trials that they dont contain the matching character. think of it as the program is learning to avoid useless looping. this is just a simple code with simple test cases but it becomes really useful if you have a big text file and you keep searching for input. – John C. Oct 27 '19 at 17:09
  • 1
    Also: you have two variables named `i`. One shadowing the other. – wildplasser Oct 27 '19 at 17:12
  • I don't think that what you propose is a useful optimization. I don't expect skipping a few bytes is faster than reading them. (And the way you determine whether you want to skip them by sifting through an unorganized large array of offsets is probably slower than either.) If your concern is optimization, read the user names to memory and organize them in a useful way. For example, sorthing the user names in memory will allow you to look up names fast with binary search. – M Oehm Oct 27 '19 at 17:19

0 Answers0