MCU - stm32f407vgt6
IDE - True Studio
Additional - CubeMx
Description -
I am trying to copy a file from USB drive to the same USB drive with diffrent name.
Source File - FILE2.txt
- This file is present in the drive and Size is 3KB
Destination File - file2copy.txt
- This file will be created in the same drive and contents of FILE2.txt
will be copied.
Code -
int CopyFile(char *srcFile, char *destFile)
{
FATFS fs0;
FIL fsrc, fdest;
BYTE buffer[4096];
FRESULT res;
UINT br, bw;
f_mount(&fs0, USBHPath, 0);
res = f_open(&fsrc, (const TCHAR*)srcFile, FA_READ | FA_OPEN_EXISTING);
if (res) return 0;
else
{
Green_Blink(100);
res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res) return 0;
else
{
Green_Blink(100);
for (;;) {
res = f_read(&fsrc, buffer, sizeof buffer, &br); /* Read a chunk of source file */
if (res || br == 0) break; /* error or eof */
res = f_write(&fdest, buffer, br, &bw); /* Write it to the destination file */
if (res || bw < br) break; /* error or disk full */
f_sync(&fdest);
}
}
}
f_close(&fsrc);
f_close(&fdest);
f_mount(0, USBHPath, 0);
return 1;
}
ERROR -
I am able to open the source file but cannot create the destination file in the drive.
res = f_open(&fdest, (const TCHAR*)destFile, FA_WRITE | FA_CREATE_ALWAYS);
if (res) return 0;
res becomes true in the case.
So my question is how do i copy file in same logical drive and what is the problem with opening the destination file in that drive.
Thanks in advance.