file transfer command in c++?

blink13

Baseband Member
Messages
31
im new to c++ and im wonderin if theres a command that can move a file from one folder to another?
 
there isn't on, (I don't think)
just open a file for reading, and then open a file for writting, read from one and write to the other... then delete the source file.
 
blink13 said:
im new to c++ and im wonderin if theres a command that can move a file from one folder to another?

The Win32 API has functions to do this within Windows. There are other cross-platform alternatives included in wxWidgets etc., but there is no native C++ function to do it.
 
you could also detect what sort of system it is, (win32 or unix/linux), then use the system command...

system("move");
system("mv");

depending on the system that is detected
 
Well, it's not C++, but here is some old C code that'll probably compile or not depending on your compiler and the available libraries and settings. You'll have to fiddle with it a bit, but it is here.

Code:
#include <stdio.h>
#include <conio2.h>

/*
  These define statements are used in calls to functions later.
 */
#define IN_F_NAME "Test.txt" /* Filename being copied from. */
#define OUT_F_NAME "Copy.txt" /* Filename being copied to. */
#define READ "rt" /* Sets up parameters for a read only text file. */
#define WRITE "wt" /* Sets up parameters for a write only text file. */

void scrhld(void)
{
     /* 
        Prompt the user to press a key to continue the program.
     */
     printf("\t\t\tPress Any Key to Continue...\n");
     
     /* 
        Wait for a key press and discard it.
     */
     getch();
}


FILE * file_open(char * name, const char * params)
{
    
     FILE * cur;
     
     
     if((cur=fopen(name,params)) == NULL)
                               {
                                    
                                     puts("Error Opening File");
                                     
                                     
                                      scrhld();
                                     
                                     
                                     exit(1);
                               } 
     
     return(cur);
}


void make_copy(FILE * cur_in, FILE * cur_out)
{
    
     char temp;
     
    
     while(0 == feof(cur_in))
                  {
                         
                         fscanf(cur_in, "%c1", &temp);               
                         fprintf(cur_out, "%c", temp);
                  }
     
}

void close_files(FILE * cur_in, FILE * cur_out)
{
     
     fflush(cur_in);
     fflush(cur_out);
     fclose(cur_in);
     fclose(cur_out);
     
}



int main(void)
{
   
    FILE * in, * out;
    
    
    in=file_open(IN_F_NAME, READ);
    out=file_open(OUT_F_NAME, WRITE);
    
    make_copy(in, out);
    
    close_files(in, out);
   
    scrhld();    
    
    return 0;
}

Works fine with Dev-C++ as a console project for C. Hope this helps.
 
Back
Top Bottom