Thread: search string into file

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53

    search string into file

    Code:
    #include <stdio.h>#include <string.h>
    #include <stdlib.h>
    
    
    void onexit(char *u, char *p, char *l, FILE *f, int flag);
    
    
    int main(int argc, char *argv[]){
        
        FILE *fp;
        char *tmp, *tmp2, *user, *pass, *line;
    
    
        printf("Inserire utente: ");
        if(scanf("%ms", &user) == EOF){
            perror("scanf");
            return EXIT_FAILURE;
        } /* scanf %ms alloca dinamicamente la memoria */
    
    
        printf("Inserire password: ");
        if(scanf("%ms", &pass) == EOF){
            perror("scanf");
            onexit(user, NULL, NULL, NULL, 1);
            return EXIT_FAILURE;
        }
    
    
        size_t max_length = strlen(user) + strlen(pass) + 3; /* +3 perchč: 1) /0 ; 2) ' ' ; 3) \n dato che line conterrā user' 'pass\0\n */
        line = malloc(max_length);
        if(line == NULL){
            perror("malloc");
            onexit(user, pass, NULL, NULL, 2);
            return EXIT_FAILURE;
        }
    
    
        fp = fopen("/home/pol/auth.txt", "r");
        if(fp == NULL){
            printf("Errore apertura file\n");
            onexit(user, pass, line, NULL, 3);
            return EXIT_FAILURE;
        }
    
    
        while(!feof(fp)){
            if(fgets(line, max_length , fp) == NULL){
                perror("fgets");
                onexit(user, pass, line, fp, 4);
                return EXIT_FAILURE;
            }
            tmp = strtok(line, " ");
            if(tmp == NULL){
                perror("strtok 1");
                onexit(user, pass, line, fp, 4);
                return EXIT_FAILURE;
            }
    
    
            tmp2 = strtok(NULL, "\n"); /* con fgets ultimo carattere č \n (se pwd=12 => 12\n) quindi devo tagliare prima di \n */
            if(tmp2 == NULL){
                perror("strtok 2");
                onexit(user, pass, line, fp, 4);
                return EXIT_FAILURE;
            }
            if((strcmp(tmp,user) == 0) && (strcmp(tmp2,pass) == 0)){
                printf("USER: %s - PASS: %s\n", tmp, tmp2);
                onexit(user, pass, line, fp, 4);
                return EXIT_SUCCESS;
            }
            else{
                continue;
            }
        }
        return EXIT_SUCCESS;
    }
    
    
    void onexit(char *u, char *p, char *l, FILE *f, int flag){
        if(flag == 1){
            free(u);
        }
        if(flag == 2){
            free(u);
            free(p);
        }
        if(flag == 3){
            free(u);
            free(p);
            free(l);
        }
        if(flag == 4){
            free(u);
            free(p);
            free(l);
            fclose(f);
        }
    }
    and this is the file auth.txt:
    Code:
    paolo 1230
    luca 1000
    gianni 0000
    If i search "paolo" or "gianni" it is all ok but if i search "luca" i got this:
    Code:
    Inserire utente: luca
    Inserire password: 1000
    strtok 2: Success
    and i don't understand why

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    There is no way that code would produce that output.

    Your IDE (or build script) has probably been confused after you have done multiple edits, and is executing an executable corresponding to an old version of code.

    Do a "Build Clean" (delete all object files and executables) and then recompile all source files from scratch before trying to execute your program again.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    no
    i've deleted the .txt files, i've deleted the executable file, i've rebooted my notebook...always got this error when trying to test the 2nd name! Only the 1st and the 3rd work!

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > if(scanf("%ms", &user) == EOF)
    > if(scanf("%ms", &pass) == EOF)
    Just where were you allocating memory for these "pointers"?
    All I see is uninitialised char* pointers.

    Did you use gcc to compile this code? If so, use the -Wall option, and heed the warnings about messing up scanf calls.

    And just what kind of "format" is "%ms" anyway?

    I suggest you forget about your exit function until you can read strings into allocated memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    @salem %ms is a POSIX EXTENSION
    I have used gcc with
    Code:
    gcc -Wall -O3 -o file file.c
    and got no warnings

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Thanks for the link on the %ms thing

    > while(!feof(fp))
    > if(fgets(line, max_length , fp) == NULL
    Write this as
    while ( fgets(line, max_length , fp) != NULL )

    > size_t max_length = strlen(user) + strlen(pass) + 3;
    But if you have two users
    fred dino
    barney myPasswordIsMuchMoreSecure

    then you're not going to read longer lines properly with the fgets() you have.

    Generally, use
    char buff[BUFSIZ];
    while ( fgets(buff, BUFSIZ , fp) != NULL )


    Initialise relevant pointers to NULL
    FILE *fp = NULL;
    char *tmp, *tmp2, *user = NULL, *pass = NULL, *line = NULL;

    Then you can do this
    Code:
    void onexit(char *u, char *p, char *l, FILE *f){
            free(u);
            free(p);
            free(l);
            if ( f ) fclose(f);
    }
    Calling free(NULL) is a safe operation.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User
    Join Date
    May 2012
    Location
    Italy
    Posts
    53
    Thanks a lot
    i've corrected my fgets and now all is working

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How to search through a file for a certain string?
    By tranman1 in forum C Programming
    Replies: 5
    Last Post: 03-08-2010, 11:59 PM
  2. search a string in a file
    By hjr in forum C Programming
    Replies: 13
    Last Post: 10-11-2009, 04:13 PM
  3. search for text string in a file
    By basenews in forum C++ Programming
    Replies: 2
    Last Post: 05-03-2007, 05:15 AM
  4. string search and copy in a file !
    By Shady in forum C++ Programming
    Replies: 33
    Last Post: 09-19-2006, 10:59 AM
  5. search a file for a string?
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 12-06-2001, 07:24 PM

Tags for this Thread