title: C++ Copying files to USB Device
body: <p>I have serveral files of different types. <code>.gz</code> <code>.cfg</code> <code>.tar</code> and I would like to <strong>copy</strong> them to a <strong>USB</strong> Device connected to a Drive. This is my code:</p>
  
  <pre><code>//path -&gt; destination path looking like: "\\\\?\\"+ driveLetter + ":\\file"
  //i -&gt; my Iterator for Map&lt;srcPath,destPath&gt;
  QFile qsrcFile(i.key());
      QFileInfo srcInfo(qsrcFile);
      QString path(i.value());
      HANDLE usbHandle = CreateFileW(path.toStdWString().c_str(),
                                  GENERIC_WRITE, //can cause permission problems &gt; set to 0
                                  FILE_SHARE_WRITE,
                                  0,
                                  CREATE_ALWAYS,
                                  FILE_ATTRIBUTE_NORMAL,
                                  0);
  
      if(usbHandle == INVALID_HANDLE_VALUE){
          DWORD err = ::GetLastError();
          fprintf(stderr,"Error INVALID_HANDLE_VALUE \n \t Path: %s \n \t Error: %d \n",path.toStdString().c_str(),err);
          if(err == ERROR_PATH_NOT_FOUND){
              CreateDirectoryW(path.toStdWString().substr(0,path.lastIndexOf("\\")).c_str(),0);
              i.previous(); //try creating a file again, 
                            //this time with a parent directory (works only for depth = 1)
          }
          continue;
      }
  
      DWORD written = 0;
  
      if(!WriteFile(usbHandle, qsrcFile.readAll(), srcInfo.size(), &amp;written, NULL)){
          qsrcFile.close();
          fprintf(stderr,"Error at WriteFile() \n \t file: %s \n",qsrcFile.fileName().toStdString().c_str()); //writting error
          continue;
      }
  </code></pre>
  
  <p>If I run the programm it seems that the usbHandler is invalid. Im not quiet sure how to use these WinAPI function properly but I thought this could work.
  I just need to write my Files to <code>I:/(USB)</code>. </p>
  
  <p>How do I copy Files to a USB device connected to a Drive?</p>
  
  <p><strong>EDIT</strong>:<p></p>
  
  <ul>
  <li><strong>FIX</strong> <code>\\\\.\\I:\\</code> to get <code>\\.\I:\</code> worked fine. (also "?" instead of dot is possible)</li>
  <li>I had the Error: <code>ERROR_ACCESS_DENIED</code> therefor I tried changing the
  <code>dwDesiredAccess</code> Flag to 0 (I also tried running VS2010 as Admin). Its fixed for now, but that caused other problems </li>
  <li>Once I (magicly) got some files , but the <code>.tar</code> and <code>.gz</code> files where broken/empty. How can I write into this files when <code>WriteFile()</code> is too primitive for that?</li>
  </ul>