tags:

views:

114

answers:

5

I have the following code to copy files

sprintf(command, "copy /Y %s %s", sourceFile, targetFile);
system(command);

It works except for the dos window showing up which is very annoying.

I am trying to use CreateProcess() (with an #ifdef for WINNT), but not sure how to setup the command line for the same. Any other options for copying files in C (on windows) without showing dos window?

+1  A: 

Here's some code I lifted from this website. You can wrap it into your own function and just pass the source and destination file paths (in this example, argv[1] and argv[2)

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

int main(int argc, char *argv[])
{
  FILE *from, *to;
  char ch;


  if(argc!=3) {
    printf("Usage: copy <source> <destination>\n");
    exit(1);
  }

  /* open source file */
  if((from = fopen(argv[1], "rb"))==NULL) {
    printf("Cannot open source file.\n");
    exit(1);
  }

  /* open destination file */
  if((to = fopen(argv[2], "wb"))==NULL) {
    printf("Cannot open destination file.\n");
    exit(1);
  }

  /* copy the file */
  while(!feof(from)) {
    ch = fgetc(from);
    if(ferror(from)) {
      printf("Error reading source file.\n");
      exit(1);
    }
    if(!feof(from)) fputc(ch, to);
    if(ferror(to)) {
      printf("Error writing destination file.\n");
      exit(1);
    }
  }

  if(fclose(from)==EOF) {
    printf("Error closing source file.\n");
    exit(1);
  }

  if(fclose(to)==EOF) {
    printf("Error closing destination file.\n");
    exit(1);
  }

  return 0;
}
Jacob
What? No `sendfile`? Hmph.
Aviral Dasgupta
The code is bogus - it can't copy an empty file.
anon
mosg
@Neil: Why doesn't it copy empty files?
Jacob
@mosg: Yeah, if you can use those API, you should, I'd recommend it.
Jacob
@Jacob For empty file, first feof() test is meaningless. Then fgetc() fails, then ferror() indicates that error, then the program errors out. Basically whoever wrote this had a poor grasp of how feof() works, and it's a godawful way to copy files in any case.
anon
@Neil: Actually, `fgetc()` does not fail. It returns `ch` such that `int(ch)` is `-1` and **then** `feof(from)` successfully prevents anything being written to the destination and we exit the while loop. So it works, but is this bad design?
Jacob
@Jacob Looks like you are right about ferror - its a long time since I used C file streams. However the code seems overly complicated - why not simply test the return value of fgetc()? And it doesn't close files correctly on error, which won't matter for a stand-alone program, but would for code used as part of another program.
anon
@Neil: Agreed, there are issues with the code.
Jacob
A: 

There are libraries that can do it, or you can write the code yourself, using a buffer and fread/fwrite. It's been a long time when I last wrote a C code, so I can't recall the exact syntax.

petersohn
+2  A: 

Use ShellExecute with SW_HIDE http://msdn.microsoft.com/en-us/library/bb762153(VS.85).aspx

Chris Thornton
How do we copy using ShellExecute(). Didn't find that operation.
vinaym
+8  A: 

Windows provides the CopyFile family of APIs for this.

Alex K.
This is what I was looking for. Thanks.
vinaym
A: 
    #include<windows.h>
    #include<tchar.h>
    #include<shellapi.h>


    #define _UNICODE

    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance,
            PSTR szCmdLine, int iCmdShow)
    {

        SHFILEOPSTRUCT s={0};

        s.hwnd = GetFocus();
        s.wFunc = FO_COPY;
        s.pFrom = _T("d:\\songs\\vineel\\telugu\0\0");
        s.pTo = _T("d:\0");
        s.fFlags = 0;
        s.lpszProgressTitle = _T("Vineel From Shell - Feeling the power of WIN32 API");

        SHFileOperation(&s);
    }

The above code will invoke explorer copy handler.....

Vineel Kumar Reddy