Posted on 2011-07-01 09:55
托雷宽 阅读(4318)
评论(0) 编辑 收藏 引用 所属分类:
Visual C++
#include <iostream>
#include <fstream>
#include <afx.h>
using namespace std;
int main()
{
char path_buffer[_MAX_PATH];
char drive[_MAX_DRIVE];
char dir[_MAX_DIR];
char fname[_MAX_FNAME];
char ext[_MAX_EXT];
GetModuleFileName(NULL,path_buffer,MAX_PATH);//得到当前路径
_splitpath( path_buffer, drive, dir, fname, ext );//拆分当前路径
cout<<path_buffer<<endl;
CString cStr,temp;
temp.Format("%s",drive);
cStr+=temp;
temp.Empty();
temp.Format("%s",dir);
cStr+=temp;
temp.Empty();
temp.Format("%s","\\aa.jpg");//将路径拼接成想要的文件路径
cStr.Delete(cStr.GetLength()-1,1);//去掉不包含文件名的路径的最后一个\,否则编译器会误会有转义字符
cStr+=temp;
temp.Empty();
cStr.Replace("\\","\\\\");
cStr.TrimRight();
//cout<<cStr<<endl;
const char* filename =cStr.GetBuffer(sizeof(cStr));//将路径赋值给一个const char* 的常量
//cout<<filename<<endl;
ifstream ifile(filename,iostream::binary);//使用
ofstream ofile("D:\\aa.jpg",iostream::binary);
try
{
if (ifile==NULL)
{
throw -1;
}
ofile<<(ifile.rdbuf());
}
catch (int e)
{
cout<<"error"<<endl;
}
ifile.close();
ofile.close();
system("pause");
return 0;
}