0

I've finished all the TODO's now, but I'm having trouble concatenating absolute path and root.

I've been all day trying different ways to do it, but still doesn't work. I hope someone can help.

Here's my code:

        char path[strlen(root) + absp_len + 1];

        strcpy(path, root);
        strcat(path, abs_path);

        printf("path:%s,", path);

here's are the contents of absolute path and root:

abs_path: /hello.php
root: /home/jharvard/Dropbox/CS50/pset6/public

and here is the program output:

path: /home/jharvard/Dropbox/CS50/pset6/public
  • Your description of the problem is too vague. What exactly is the problem? Please be precise. Can you post the contents of root and abs_path along with the printf output? We need to know what the inputs and outputs are to get an idea of what is going wrong. You might also try printing out the contents of root and abs_path both before and after the strcpy and strcat commands to make sure nothing is being overwritten and to confirm their contents. Also, when you declare char path, are you allowing room for the end of string marker? – Cliff B Oct 21 '15 at 2:50
  • Sorry for that, I edited my question including the program output. Everything seems to be correct when I print it, except for path. I also tried making a little test program equal to this which works. – Belu Oct 21 '15 at 3:02
  • Your printf is printing root, not path – Sam Gerber Oct 21 '15 at 3:05
  • same happens when I print path, I'm sorry, I changed it so many times I forgot to change that to post it in here – Belu Oct 21 '15 at 3:08
2

Belu, I tried this code:

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

int main(void)
{
    char* abs_path = "/hello.php";
    char* root = "/home/jharvard/Dropbox/CS50/pset6/public";

    char path[strlen(root) + strlen(abs_path) + 1];

    strcpy(path, root);
    strcat(path, abs_path);

    printf("path:%s\n", path);
    return 0;
}

As follows:

jharvard@appliance (~/Dropbox/pset6): make test clang -ggdb3 -O0 -std=c99 -Wall -Werror test.c -lcs50 -lm -o test
jharvard@appliance (~/Dropbox/pset6): ./test path:/home/jharvard/Dropbox/CS50/pset6/public/hello.php

Maybe you can try to compare your code and see how it's different, as I'm not able to replicate the result you're seeing.

| improve this answer | |
  • 1
    I found the problem, I was extracting absolute path inside an if statement so it was out of scope when I tried to concatenate. I took a while to found because I had declared absolute path as a global variable, and I thought that when I extracted the path, it would be stored in that global variable. Thankyou for your help, now pset6 is working! – Belu Oct 21 '15 at 4:16

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .