简体   繁体   中英

Segmentation fault when using strlen

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.

PS 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");
}
}

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';

try

fscanf(file, "%24s", read_string);

If your crash is not reproduced any more, that means that your file contains lines with more than 24 charcters. and in this case you can make your buffer bigger to avoid this problem

EDIT:

Try to add input[i] = '\\0'; after the for loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM