0

I'm a beginner in C++ and I'm trying to make a file manager used with commands. Everything was good except for the copy function.

#include <stdlib.h>
int main(int argc, char * argv[])
{
    system(*"copy \"" + argv[0] + *"\" \"C:/Users/doubl/Desktop\"");
}

There isn't an error when compiling and running, but it never actually transfers anything.

Is there a better way to do this? if not what am I doing wrong here?

  • 2
    You cannot concatenate strings like that. Also, putting in `*` in front of things randomly might get rid of errors, but it's not going to fix anything. – ChrisMM Jun 06 '22 at 18:56
  • 1
    Why do you have pointers to strings in the system() call? – mhopeng Jun 06 '22 at 18:57
  • 1
    There [is a better way](https://en.cppreference.com/w/cpp/filesystem/copy) also. – Drew Dormann Jun 06 '22 at 18:57
  • 1
    To fix your actual problem, it's easiest to convert to a C++ string first. `system((std::string("copy \"") + argv[0] + "\" \"C:/Users/doubl/Desktop\"").c_str());`. As DrewDormann mentioned though, there's already this functionality built into the language. – ChrisMM Jun 06 '22 at 18:59
  • 1
    While `"copy \""` is a C-style string, `*"copy \""` is the number `99`, the ASCII value for `c`, when added to a pointer. – Drew Dormann Jun 06 '22 at 19:01
  • 1
    It should also be noted that `argv[0]` is the name of your program, not the first argument passed to the program. – Drew Dormann Jun 06 '22 at 19:04
  • @DrewDormann When I printed `argv[0]` it showed the full path of the executable, and I was just testing so I used that. – Nicholas Picklas Jun 06 '22 at 19:12
  • Unrelated Note: You can't count on the full path being in `argv[0]`. It could also contain a relative path, a symlink, whatever the user typed, or whatever command was executed from a program or script. In some cases, usually bare metal embedded systems, it can be an empty string. – user4581301 Jun 06 '22 at 19:18
  • @user4581301 Then how would I get the executable path? Is there a more reliable way or am I doomed to count on `argv[0]` and just hope it has what I'm looking for – Nicholas Picklas Jun 06 '22 at 19:23
  • That, unfortunately, is target specific. Every system does it a little differently. Here is a question on the topic with answers that cover the most common systems that you'll encounter: [Get path of executable](https://stackoverflow.com/questions/1528298/get-path-of-executable) – user4581301 Jun 06 '22 at 19:29
  • I recommend using an Operating System function, don't go through `system`. The operating was designed to efficiencly copy files and already has the code. There's an overhead when using `system`. – Thomas Matthews Jun 06 '22 at 20:21

1 Answers1

-2

A possible way to copy files in C++ is to learn then use some existing C++ library or framework, like Qt or POCO.

Another way is to use C++ std::string-s, documented here to build your command line.

Newer C++ standards have std::filesystem documented here.

Of course, you will spend days in reading documentation.

My recommendation is to read the documentation of your C++ compiler (e.g. GCC, that you might invoke as g++ -Wall -Wextra -g - all warnings and debug info). Then read the documentation of your debugger (e.g. GDB).

You probably also want to read the documentation of your version control system (e.g. git).

Notice that your code (even improved a bit) won't work on my computers (they all run Linux), since copy (as a program) does not exist on them. The near equivalent program is called /bin/cp part of GNU coreutils.

I heard that copy is a Windows specific program. It existed on MSDOS.

At last, some computers (e.g. RaspberryPi) might be programmable in C++ and not even have any files.

Basile Starynkevitch
  • 223,805
  • 18
  • 296
  • 547