Hi everyone,

I was wondering if anyone could help me - at the minute I have a c++ program that renames a file and then attempts to move it too a new folder using linux commands and passing them to the 'system()' function. It renames the files no problem - however, it doesn't seem to be able to move them!

It gives no errors for the move command so I'm assuming it's fine, but is there any other reason that it might not be able to move a file?

Any help would be much appreciated :)

Recommended Answers

All 4 Replies

Have you giving file relative path or absolute path ?

Could you send your code ?

While system() commands can be convenient to write, they are quite inefficient (your program is running under one shell, and each system() first spawns a new shell, then runs the specified command in that shell), and their use is generally considered poor programming practice.

Instead, consider using the rename() function to rename and move the file in one step (if that is possible on your system, it may require that the source and destination locations be on the same disk partition). If that fails, you may have to copy the contents of the file to the new location (open old file for reading, new file for writing, read data from old and write to new until there's no more data to read, close both files), and then delete the original file using remove() (reference can be found on the same site as above).

Hi everyone,

Thanks for the replies - I got it working eventually, but I don't know how :S What I had was a function that you passed the original file, and the output path you wanted to file to be moved too. The function then used a string stream to make the mv command, then returned the string as a char* back to the main program and system().

Now, that never worked - however, I changed the function to no longer return the command, but to run it from within the function and it worked!!

Never knew about the rename() function in C++, might give it a try too see if its any better, thanks everyone :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.