Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Problem trying to copy string char by char

I’m trying to travel a char array char by char and copy it into another char array until the read character is \ because I want to create the directories of the adress if not created or access if they are created.

For example:

Adress: dir2/dir3/a

My algorithm:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

path = dir2
//if dir2 not created make dir2 and change actual directory to dir2
//if created then access dir2 changing the actual directory to dir2
//empty path with memset and keep storing the adress
path = dir3
//repeat...

But when I try to access path I get the last character and not all the path string

Output:

path0: d
path1: i //Should be di
path2: r //Should be dir
path3: 2 //Should be dir2
path: 2 //Should be dir2

I don’t know if there is a more efficient way to do this and I don’t know how to get the complete path string and not the last character, I was inserting ‘\0’ at the end just in case it was some problem with the string end character

name and path are a char[256] variable
Code:

for (i = 0; i < strlen(name); i++) 
        {
            if(name[i] != '/')
            {
                path[j] = name[i];
                path[i+1] = '\0';
                printf("path%d: %s\n", i,path);
            }
            else
            {
                path[i] = '\0';
                printf("path: %s\n", path);
                n = chdir(path);
                if(n == ENOENT)
                {
                    n = mkdir(path, 0777);
                    if (n != 0) 
                    {
                        fprintf(stderr, "Failed to make the directory to extract: %s\n", strerror(errno));
                        close(fmypack);
                        return E_DIR1;
                    }
                    chdir(path);
                }
                else if(n == EACCES)
                {
                        fprintf(stderr, "Failed to access the directory: %s, %s\n", path, strerror(errno));
                        close(fmypack);
                        return E_DIR2;
                }
                else if(n != 0)
                {
                        fprintf(stderr, "Unknow error when try to access the directory: %s, %s\n", path, strerror(errno));
                        close(fmypack);
                        return E_DESCO;
                }
                memset(path, 0, sizeof path);
                j=0;
            }

>Solution :

I think I found it.

                path[i] = '\0';

But path is built up using j as an indexer.

                path[j] = '\0';

Should be correct.

And you’re missing your increment on j:

                path[j] = name[i];
                path[i+1] = '\0';

Should be:

                path[j++] = name[i];
                path[j+1] = '\0';

What’s funny is it actually is faster to do this one component at a time with chdir(), but the probability that you have found this is remote. When you get a couple hundred directories deep you can measure it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading