-1

I have searched for all the possible answers to this problem; however, I still get the error. I have a server.conf file it has some values and I assign the values in this file to variables in a structure. I want to use the path variable in the struct as the filename in fopen. I know this problem has been asked about several times, but I still couldn't recognize what's the problem after days.

#include <stdio.h>
#include <stdlib.h>

typedef struct
{
    char *path;

} MainStruct;


int parse_config(MainStruct *inf)
{

    FILE *file;
    char *line = NULL;
    char *path;
    size_t len = 0;
    ssize_t read;

    file = fopen("server.conf", "r");
    if (file != NULL)
    {

        while ((read = getline(&line, &len, file)) != -1)
        {

            if (strstr(line, "PATH") != NULL)
            {
            
                inf->path = strdup(line + 5);
            }
        }
        fclose(file);
    }
    else
        return 0;

    return 1;
}


int main(int argc, char *argv[])
{
    MainStruct val;

    parse_config(&val);
    char *pt = val.path;
    printf("%s", pt);
    FILE *fp = fopen(pt, "r");

    if (fp == NULL) {
        fprintf(stderr, "fopen() failed in file %s at line # %d \n", __FILE__,__LINE__);
        return EXIT_FAILURE;
    }

    fclose(fp);

}

this is the content of the server.conf file where I defined the PATH:

PATH file.html

I have checked the value of pt with printf and it's exactly the string file.html.

Nafis
  • 3
  • 3
  • 2
    Have you done any debugging? It would help if you provide info about obvious things that anyone debugging the issue would want to know. For example, have you checked the value of `pt`? Is it the string value you expect? Is `file.html` in the same directory as you are running the executable from? etc. – kaylum Jul 20 '22 at 08:22
  • 7
    The string will, essentially, be `"\"file.html\""`, and I doubt the file in the file-system really have the double-quotes. Remove the quotes in the `server.conf` file. – Some programmer dude Jul 20 '22 at 08:23
  • 1
    For future problems like this, please learn [how to *debug* your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). Either by simply printing out the contents and values of variables, or by using a [*debugger*](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) to step through the code statement by statement while monitoring variables and their values. – Some programmer dude Jul 20 '22 at 08:26
  • 1
    The line read by `getline(...)` will have a trailing newline character. You need to remove it. – Ian Abbott Jul 20 '22 at 08:45
  • @kaylum, yes I have checked the value of pt and it is exactly what I want, it is "file.html" – Nafis Jul 20 '22 at 08:57
  • I have also removed all the trailing spaces using a trim function so be sure that pt is exactly the string I want – Nafis Jul 20 '22 at 08:59
  • 1
    Quotes are only needed in the *source code*, not in the strings you read from file. – Some programmer dude Jul 20 '22 at 09:16
  • Please post a [mcve]. Note we do not need to see how you do `atoi`, so you may remove this from your example. Just read the `PATH` entry and open the file. – n. m. could be an AI Jul 20 '22 at 09:29
  • 1
    Note also that `perror("fopen()")` is not very informative. Include the file name you are trying to open in the message. – n. m. could be an AI Jul 20 '22 at 09:31
  • A [mcve] should include *all* the code needed to reproduce the problem. You only posted a fragment without `main` and without any necessary `#include` directives. Editing them out was a wasted effort. Make a single `.c` file, compile it, run it, verify that it does what you say it does, *then post the entire file, unedited*. – n. m. could be an AI Jul 20 '22 at 10:12

2 Answers2

2
#include <stdio.h>
#include <string.h>

int main() {
    printf("%s\n", strdup("PATH \"file.html\"" + 5));
}
// output: "file.html"

If the value of pt is "file.html" then the fopen() will try to open a file that is named "file.html" instead of a file named file.html.

Unless there is a file where the filename itself includes the double quotes, fopen() will fail.

Edit: added example code

makeVoiceBot
  • 143
  • 7
0

man getline:

getline() reads an entire line from stream, storing the address of the buffer containing the text into *lineptr. The buffer is null-terminated and includes the newline character, if one was found.

So your path is likely to point to a buffer that contains file.html\n, not file.html. It could be rather confusing if you printf it and don't pay attention to how many newlines are printed at the end.

n. m. could be an AI
  • 112,515
  • 14
  • 128
  • 243