使用 GetOpenFileName 获取文件路径后,CopyFile API 函数不起作用
CopyFile API function not working after using GetOpenFileName to get the file path
我正在使用 GetOpenFileName()
to get the file path, and then CopyFile()
以不同的文件名将文件复制到同一目录。
通过使用 GetLastError()
,我得到一个错误代码:
0x2 - The system cannot find the file specified.
但是当我使用 MessageBox()
查看文件名时,它显示了正确的文件名。
除此之外,我也试过StrTrim()
文件名,但还是不行。
我正在使用 Borland C++Builder 6.0。
代码
OPENFILENAME ofn;
char szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Form1->Handle;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '[=11=]';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All[=11=]*.*[=11=]Text[=11=]*.txt[=11=]";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = 0;
GetOpenFileName(&ofn);
LPSTR FileName;
FileName = ofn.lpstrFile;
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, strcat(FileName, ".newfile"), "New File Name", MB_OK);
CopyFile(FileName, strcat(FileName,".newfile"), false);
已编辑代码
我试过使用两个变量,但还是不行。谁能提出更正建议?
LPSTR FileName;
LPSTR NewFileName;
FileName = ofn.lpstrFile;
NewFileName = FileName;
strcat(NewFileName, ".newfile");
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, NewFileName, "New File Name", MB_OK);
CopyFile(FileName, NewFileName, false);
工作代码
下面的代码有效,但我必须添加一个文本框/编辑控件。有没有办法在不添加任何额外控制的情况下做到这一点?
LPSTR FileName;
AnsiString NewFileName;
FileName = ofn.lpstrFile;
Edit1->Text = FileName;
NewFileName = Edit1->Text + ".bak";
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, NewFileName.c_str(), "New File Name", MB_OK);
您正在为 CopyFile()
的两个参数使用单个缓冲区,并且您将新扩展名附加到该缓冲区,因此 CopyFile()
当然不会找到要复制的文件。
您需要为CopyFile()
的两个参数使用两个单独的缓冲区,例如:
TCHAR szSrcFile[MAX_PATH];
TCHAR szDstFile[MAX_PATH+12];
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Form1->Handle;
ofn.lpstrFile = szSrcFile;
ofn.lpstrFile[0] = TEXT('[=10=]');
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = TEXT("All[=10=]*.*[=10=]Text[=10=]*.txt[=10=]");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
lstrcpy(szDstFile, szSrcFile);
lstrcat(szDstFile, TEXT(".newfile"));
MessageBox(Form1->Handle, szSrcFile, TEXT("File Name"), MB_OK);
MessageBox(Form1->Handle, szDstFile, TEXT("New File Name"), MB_OK);
CopyFile(szSrcFile, szDstFile, FALSE);
}
话虽如此,既然你使用的是Borland的VCL框架,你应该使用TOpenDialog
组件而不是直接使用GetOpenFileName()
,然后你可以使用String
变量方便串联:
// these properties can also be set at design-time instead...
OpenDialog1->Filter = "All|*.*|Text|*.txt";
OpenDialog1->FilterIndex = 1;
OpenDialog1->Options = TOpenOptions() << ofPathMustExist << ofFileMustExist;
if (OpenDialog1->Execute())
{
String SrcFile = OpenDialog1->FileName;
String DstFile = SrcFile + ".newfile";
CopyFileA(SrcFile.c_str(), DstFile.c_str(), FALSE);
}
我正在使用 GetOpenFileName()
to get the file path, and then CopyFile()
以不同的文件名将文件复制到同一目录。
通过使用 GetLastError()
,我得到一个错误代码:
0x2 - The system cannot find the file specified.
但是当我使用 MessageBox()
查看文件名时,它显示了正确的文件名。
除此之外,我也试过StrTrim()
文件名,但还是不行。
我正在使用 Borland C++Builder 6.0。
代码
OPENFILENAME ofn;
char szFile[260];
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Form1->Handle;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '[=11=]';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = "All[=11=]*.*[=11=]Text[=11=]*.txt[=11=]";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = 0;
GetOpenFileName(&ofn);
LPSTR FileName;
FileName = ofn.lpstrFile;
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, strcat(FileName, ".newfile"), "New File Name", MB_OK);
CopyFile(FileName, strcat(FileName,".newfile"), false);
已编辑代码
我试过使用两个变量,但还是不行。谁能提出更正建议?
LPSTR FileName;
LPSTR NewFileName;
FileName = ofn.lpstrFile;
NewFileName = FileName;
strcat(NewFileName, ".newfile");
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, NewFileName, "New File Name", MB_OK);
CopyFile(FileName, NewFileName, false);
工作代码
下面的代码有效,但我必须添加一个文本框/编辑控件。有没有办法在不添加任何额外控制的情况下做到这一点?
LPSTR FileName;
AnsiString NewFileName;
FileName = ofn.lpstrFile;
Edit1->Text = FileName;
NewFileName = Edit1->Text + ".bak";
MessageBox (Form1->Handle, FileName, "File Name", MB_OK);
MessageBox (Form1->Handle, NewFileName.c_str(), "New File Name", MB_OK);
您正在为 CopyFile()
的两个参数使用单个缓冲区,并且您将新扩展名附加到该缓冲区,因此 CopyFile()
当然不会找到要复制的文件。
您需要为CopyFile()
的两个参数使用两个单独的缓冲区,例如:
TCHAR szSrcFile[MAX_PATH];
TCHAR szDstFile[MAX_PATH+12];
OPENFILENAME ofn;
ZeroMemory(&ofn, sizeof(ofn)); ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = Form1->Handle;
ofn.lpstrFile = szSrcFile;
ofn.lpstrFile[0] = TEXT('[=10=]');
ofn.nMaxFile = MAX_PATH;
ofn.lpstrFilter = TEXT("All[=10=]*.*[=10=]Text[=10=]*.txt[=10=]");
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
if (GetOpenFileName(&ofn))
{
lstrcpy(szDstFile, szSrcFile);
lstrcat(szDstFile, TEXT(".newfile"));
MessageBox(Form1->Handle, szSrcFile, TEXT("File Name"), MB_OK);
MessageBox(Form1->Handle, szDstFile, TEXT("New File Name"), MB_OK);
CopyFile(szSrcFile, szDstFile, FALSE);
}
话虽如此,既然你使用的是Borland的VCL框架,你应该使用TOpenDialog
组件而不是直接使用GetOpenFileName()
,然后你可以使用String
变量方便串联:
// these properties can also be set at design-time instead...
OpenDialog1->Filter = "All|*.*|Text|*.txt";
OpenDialog1->FilterIndex = 1;
OpenDialog1->Options = TOpenOptions() << ofPathMustExist << ofFileMustExist;
if (OpenDialog1->Execute())
{
String SrcFile = OpenDialog1->FileName;
String DstFile = SrcFile + ".newfile";
CopyFileA(SrcFile.c_str(), DstFile.c_str(), FALSE);
}