0

I have created a function which searches each line of a file and tries to match the parameter par_string with a string in the file. I have variants of the function working for doubles and ints, but I can't seem to modify the function to read in strings from file.

Currently I am using sscanf() to read in a line and my current effort is to use strcpy() to copy the string in the file to the string parameter parameter defined in the function argument. However, my function is seg faulting once I reach strcpy(), and I can't quite figure out why.

I'm hoping to be able to call the function as read_string("FILENAME", filename); in my main function to be able to pass in a parameter to the function and have the filename returned to the variable filename. I've tried defining filename using

char filename[MAXLINE]

and

char *filename = malloc(sizeof(*filename) * MAXLINE);

but both have the same problem.

Below is the function I've written. Is there anything obvious I'm missing or is there a better way to do this?

Thanks!

int 
read_string(char par_string[], char *parameter)
{
    char line[MAX_LINE], ini_par_name[MAX_LINE], par_separator[MAX_LINE];
    char par_value[MAX_LINE];

    FILE *par_file;

    if ((par_file = fopen(INI_FILE, "r")) == NULL)
    {
        printf("Cannot open parameter file 'plane.ini'.\n");
        exit(-1);
    }

    int linenum = 0;
    parameter = STRING_NO_PAR_CONST;

    while(fgets(line, MAX_LINE, par_file) != NULL)
    {        
        linenum++;

        /* 
        * If the line is a comment, skip that line. Note: There is a bug here
        * which will cause the script to crash if there is a blank line.
        */
        if (line[0] == '#' || line[0] == ' ')
        {
            continue;
        }

        /* 
        * Check a normal line is in the correct format
        */
        if (sscanf(line, "%s %s %s", ini_par_name, par_separator, par_value) \
            != 3)
        {
            printf("Syntax error, line %d for parameter %s\n", linenum,
                par_string);
            exit(-1);
        }

        /* 
        * Use strcmp to compare the difference between two strings. If it's
        * the same parameter, then strcmp will return 0. 
        */
        if (strcmp(par_string, ini_par_name) == 0)
        {
            strcpy(parameter, par_value);
        }
    }

    /* 
    * If parameter wasn't updated, the parameter was not found. Return an
    * error. 
    */
    if (parameter == STRING_NO_PAR_CONST)
    {
        printf("Parameter %s could not be found.\nExiting simulation.\n",
            par_string);
        exit(-1);
    }

    if (fclose(par_file) != 0)
    {
        printf("File could not be closed.\n");
        exit(-1);
    }

    return 0;
}
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
Saultyevil
  • 55
  • 1
  • 4

2 Answers2

1

what is this line doing?

parameter = STRING_NO_PAR_CONST;

If it is assigning parameter to point to a string literal, that is likely your problem, you cannot modify the contents of a string literal.

If STRING_NO_PAR_CONST is '\0', that is just a char, not a string. As long as parameter has correctly been allocated some memory prior to calling read_string(), you can just

parameter[0] = STRING_NO_PAR_CONST

and the same when you check to see if it has changed

if (parameter[0] == STRING_NO_PAR_CONST)
Stephen Docy
  • 4,738
  • 7
  • 18
  • 31
  • ah, I see. `STRING_NO_PAR_CONST = '\0'`. I was using something similar in another function as a check to see if the variable was found. It fixed my issue though, thanks very much! – Saultyevil Apr 17 '18 at 20:41
0

A few things

strcpy is called as the following -> strcpy(destination, source). You are using parameter as the destination.

That being said, the amount of space you allocate should be the maximum size of the string you want. I don't think that it should equal the maximum number of lines in a given file (though I could be wrong)

Eric Yang
  • 2,678
  • 1
  • 12
  • 18