Accessing directories in Linux

I'm new to Linux and trying to port
a c++ program from windows.

what I'm trying to do is copy a file to a directory off
the root of the drive

First off the program is located and running from

Drive:\Base\Web\Today\Program.exe

And trying to copy to:

Drive:\Base\cpics

windows version looks like this:

sprintf(cbuf,"copy %s \\Base\\cpics\\%s",ccc,newpic);
    system(cbuf);

Linux version:

sprintf(cbuf,"cp %s //Base//cpics//%s",ccc,newpic);
    system(cbuf);

Which of course doesn't work, hence the cry for help:)

help?

WHAT doesn't work? Any error messages? Odd behaviour? Is the source file correctly located/found (in the current working directory)?

1 Like

Why "of course"?

By the way, although I don't think this is the issue, you shouldn't double the directory separators:

    sprintf(cbuf,"cp %s /Base/cpics/%s",ccc,newpic);
    system(cbuf);

You're very, very close - close enough that I wonder if this is permissions issues rather than program ones... Or, perhaps, parts of the program we didn't see, preventing it from working.

By the way, if this is the sort of C++ program that's line after line of system(), it'd probably be more easily "ported" to shell script.

I think I understand the problem now

what windows sees as D:\Base\cpics

linux sees /media/mint/Storage/Base/cpics

And writing

sprintf(cbuf,"cp %s //media//mint//Storage//Base//cpics//%s",ccc,newpic);

isn't an option for portability reasons, as I need to copy to the same drive that the program is run from and these mount points could change.

You were correct btw, the way it was originally written was trying to access linux root directory.

Interesting, strangely it works both ways.

No errors everything compiles fine, just can't get it to move up
the directory tree 2 levels.

seems the best I can do is cp file ../ and move up one directory. I may have to rearrange my directory structure to get this to work in linux. Thanks for all your suggestions

You certainly won't need to modify the directory structure as any *nix system is very versatile in and capable of moving files around.
Maybe your approach would benefit from slight adaptions, e.g. as proposed by Corona668?

If your referring to this

It's a c++ program with roughly 300 lines of code and only
one system() call

Okay then.

If cp file ../ (or cp file .. ) works to move the file up one level, then cp file ../.. will work to move it up two levels (with obvious exceptions for being too high in the filesystem hierarchy and symlinks).

1 Like

And you sir have nailed it and the answer was so simple, thank
you very much Don.