How can I copy a file from one directory to another in c/c++

前端 未结 7 423
闹比i
闹比i 2020-12-17 07:18

I am looking for a simple example on how to copy a file from one directory to another in C. The program should only use cross platform functions that are native to C.

7条回答
  •  清歌不尽
    2020-12-17 07:59

    Another alternative is something like this:

    #ifdef SOME_OS
      #define COPY_STR "copy %s %s"  // some sort of OS-specific syntax
    
    #elif defined SOME_OTHER_OS
      #define COPY_STR "%s cpy %s"   // some sort of OS-specific syntax
    
    #else
      #error "error text"
    #endif
    

    ...

    #include    //sprintf()
    #include   //system()
    
    char copy_str[LARGE_ENOUGH];
    char* source;
    char* dest;
    ...
    
    sprintf (copy_str, COPY_STR, source, dest);
    system (copy_str);
    

提交回复
热议问题