Segmentation fault when using strlen

11,655

Im reading from a file and saving it into the char array as a string but when i try using stelen on the string, it crashes with a segmentation fault.

You don't need to take the address of read_string in fscanf, because read_string decays already to a pointer. Anyway, since &read_string == read_string in the case of an array (except for types), it shouldn't cause segmentation fault.

Therefore the problem comes from elsewhere. Among other, check that the line in your file is not larger than 25 characters.

Another problem is when i copy contents of read_string into another char array, it adds radom characters at the end.

Don't forget to add a '\0' at the end of your string.

input[i] = '\0';
Share:
11,655
blaz1nthunder
Author by

blaz1nthunder

Updated on June 04, 2022

Comments

  • blaz1nthunder
    blaz1nthunder almost 2 years

    I've been working on this for past two days but can't seem to figure out the problem.

    I'm reading from a file and saving it into the char array as a string but when I try using strlen() on the string, it crashes with a segmentation fault.

    Here is what I'm doing.

    char read_string[25];
    fscanf(file, "%s", &read_string);
    int length = strlen(read_string);
    

    Another problem is when I copy contents of read_string into another char array, it adds random characters at the end.

    Here is what I am doing

    char input[25];
    for(i=0; i<25; i++)
    {
        if (read_string[i] == '.')
            break;
        else
            input[i] = read_string[i];
    }
    

    I need to copy everything before the period is read string into input array.

    P.S. I am using fscanf() because the file is well structured.

    Here is my code if needed.

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    main()
    {
    //variables
    FILE *rFile; 
    int num_states;                 //number of states
    int num_alphabet;               //number of alphabets
    int num_acstates;               //number of accepting states
    int ac_state;                   //the accepting state
    char alphabet[num_alphabet];    //alphabet
    char read_string[25];   //assuming the input will be maximum of 25 character
    char input[25];
    int i,j,x;                      //indexes for the loop
    
    
    //open the file to read
    rFile = fopen("dfa11", "r");
    
    if (rFile != NULL)
    {
        //do the program here
    
        //read number of states and number of alphabets
        fscanf(rFile, "%d %d", &num_states, &num_alphabet);
    
        //read the alphabet
        fscanf(rFile, "%s", &alphabet); 
    
        //read the state transition table (matrix)
        int value = num_states*num_alphabet;
        char table[num_states][num_alphabet];
    
        for(i=0; i<num_states; i++)
        {
            for(j=0; j<num_alphabet; j++)
            {
                fscanf(rFile, "%d", &table[i][j]);
            }
        }
    
        //read number of accepting states and accepting state
        fscanf(rFile, "%d", &num_acstates);
        fscanf(rFile, "%d", &ac_state);
    
        //read the input string from the file
        fscanf(rFile, "%s", &read_string);
    
        for(i=0; i<25; i++)
        {
            if (read_string[i] == '.')
                break;
            else
                input[i] = read_string[i];
        }
    
        //strncpy(input, read_string, j);
    
        printf("%d\n", strlen(read_string));
        printf("%d\n", strlen(input));
        printf("%s\n", read_string);
        printf("%s\n", input);
    
    
        //close the file
        fclose(rFile);
    }
    
    else
    {
        printf("File could not be found");
    }
    }
    
  • blaz1nthunder
    blaz1nthunder over 11 years
    Thanks, I tried &read_string and read_string. None of them make any difference to my problem. Also what do you mean add \0 at the end of the string? The string comes from a file.
  • blaz1nthunder
    blaz1nthunder over 11 years
    Thanks but didnt make any difference.
  • MOHAMED
    MOHAMED over 11 years
    If you tried and the crash is not reproduced any more that means the caus of crash is not the overflow when reading from file
  • md5
    md5 over 11 years
    @user1993422: There are a lot of errors in your source code. You could try to write a SSCCE code. For instance, num_alphabet is not initialized and used as size of alphabet array. Moreover you surely know that every string should end with '\0'. Here, input has no such character.
  • blaz1nthunder
    blaz1nthunder over 11 years
    Yep, the cause isnt overflow. But I cant just find what the problem is in my program. I retyped the program 3 times already. Can you please glance through my program and suggest any changes i should make? Ill appriciate it alot!
  • blaz1nthunder
    blaz1nthunder over 11 years
    OMGG Thanks alot man, null terminating the string did the trick!! Can't believe it never occured to me. Also I am planning to use those variables as soon as this error gets fixed. Program still isnt done.
  • blaz1nthunder
    blaz1nthunder over 11 years
    But I still cant get the strlen(read_string) to work. As it still causes seg fault.
  • md5
    md5 over 11 years
    @user1993422: Did you try to write a SSCEE code, as I said? Sure this will solve the problem. ;-)
  • blaz1nthunder
    blaz1nthunder over 11 years
    Got strlen to work as well :) Tried SSCEE code, guess its my new best friend. Problem was with my variables, changed the order a bit and that did the job.