11 个解决方案
#1
open()
read()
write()
read()
write()
#2
#include <windows.h>
#include <stdio.h>
#include <fstream.h>
#define ERRCODE DWORD
#define BUFFERSIZE 36*1024
#define COPY_VERSION "1.0"
#define err_OK 0
#define err_PARAMETER 1
#define err_OPENFILE 2
#define err_HANDLESCREEN 3
#define err_GETCURSOR 4
void ShowHelp(void);
void ShowUseage(void);
BOOL isFullPath(char* pszPath);
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName);
void ShowPer(DWORD per, DWORD filesize);
void ShowProcess(DWORD dwPer, DWORD filesize);
//useage:Copynew *** to ***
int main(int argc , char* argv[])
{
//参数校验设计
ERRCODE errCode = err_OK;
switch(argc)
{
case 1:
ShowHelp();
return err_OK;
case 2:
if((strcmp(argv[1], "/?") == 0)
|| (strcmp(argv[1], "/h") == 0)
|| (strcmp(argv[1], "-h") == 0))
{
ShowHelp();
errCode = err_OK;
return errCode;
}
else
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
case 4:
if(strcmp(argv[2],"to") != 0)
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
break;
default:
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
//功能代码设计
char* pszSrcFileName;
BOOL bIsFullPath;
bIsFullPath = isFullPath(argv[1]);
if (bIsFullPath)
{
pszSrcFileName = argv[1];
}
else
{
char szBuf[MAX_PATH];
pszSrcFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszSrcFileName);
pszSrcFileName = StrCat_to_FullPath(pszSrcFileName, argv[1]);
}
char* pszDstFileName;
bIsFullPath = isFullPath(argv[3]);
if (bIsFullPath)
{
pszDstFileName = argv[3];
}
else
{
char szBuf[MAX_PATH];
pszDstFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszDstFileName);
pszDstFileName = StrCat_to_FullPath(pszDstFileName, argv[3]);
}
ifstream infile(pszSrcFileName, ios::binary);
if(infile.fail() == 1)
{
cout<<"can't open file "<<pszSrcFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
ofstream outfile(pszDstFileName, ios::binary);
if(outfile.fail() == 1)
{
cout<<"can't open file "<<pszDstFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
HANDLE handleScreen;
handleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
if(!handleScreen)
{
return err_HANDLESCREEN;
}
COORD coordCursor;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
CONSOLE_CURSOR_INFO CursorInfo;
char Buffer[BUFFERSIZE];
HANDLE fSrcHandle;
WIN32_FIND_DATA FileData1;
fSrcHandle = FindFirstFile(pszSrcFileName,&FileData1);
DWORD dwSrcFileSize = FileData1.nFileSizeLow;
DWORD dwWrited = 0;
printf("\n################## File Copy Start ! ##################\n\n");
do
{
infile.read(Buffer,sizeof(char)*BUFFERSIZE);
DWORD dwReaded=infile.gcount();
outfile.write(Buffer,infile.gcount());
dwWrited += dwReaded;
ShowPer(dwWrited,dwSrcFileSize);
ShowProcess(dwWrited,dwSrcFileSize);
GetConsoleScreenBufferInfo(handleScreen, &ScreenBufferInfo);
coordCursor = ScreenBufferInfo.dwCursorPosition;
coordCursor.Y = coordCursor.Y-2;
CursorInfo.dwSize = 66;
CursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(handleScreen, &CursorInfo);
SetConsoleCursorPosition(handleScreen,coordCursor);
} while(!infile.eof());
infile.close();
outfile.close();
printf("\n\n\n\n################# File Copy Completed ! #################\n\n");
return err_OK;
}
//自定义函数行
void ShowHelp(void)
{
printf( "\n"
"Copy file. v.%s\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n"
"Copynew [/? | /h | -h]\n"
"\n"
"/? /h -h : 显示帮助信息.\n"
"srcFileName : 源文件的路径.\n"
"dstFileName : 目的文件路径.\n"
"\n",COPY_VERSION);
}
void ShowPer(DWORD dwPer, DWORD filesize)
{
DWORD show = (DWORD)(100*((float)dwPer/filesize));
printf("\r");
printf(" %d%% of the file has been copied.\n\n", show);
}
void ShowUseage(void)
{
printf( "\n"
"Copy file.v%s.\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n",COPY_VERSION);
}
void ShowProcess(DWORD dwPer, DWORD filesize)
{
printf(" $:==================================================:$");
printf("\r");
DWORD show = (DWORD)(100*((float)dwPer/filesize))/2;
printf(" $:");
for (DWORD i = 0; i<show ;i++)
{
printf(">");
}
}
BOOL isFullPath(char* pszPath)
{
DWORD dwcount;
dwcount = strlen(pszPath);
for(DWORD dwi = 0; dwi < dwcount; dwi++)
{
if(*pszPath == ':')
return TRUE;
else
pszPath ++ ;
}
return FALSE;
}
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName)
{
pszCurDirectory = strcat(pszCurDirectory,"\\");
pszCurDirectory = strcat(pszCurDirectory,pszFileName);
return pszCurDirectory;
}
以前写的程序,带console窗口进度显示的拷贝程序
你改改的用吧
个人意见,高手多多指教
#include <stdio.h>
#include <fstream.h>
#define ERRCODE DWORD
#define BUFFERSIZE 36*1024
#define COPY_VERSION "1.0"
#define err_OK 0
#define err_PARAMETER 1
#define err_OPENFILE 2
#define err_HANDLESCREEN 3
#define err_GETCURSOR 4
void ShowHelp(void);
void ShowUseage(void);
BOOL isFullPath(char* pszPath);
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName);
void ShowPer(DWORD per, DWORD filesize);
void ShowProcess(DWORD dwPer, DWORD filesize);
//useage:Copynew *** to ***
int main(int argc , char* argv[])
{
//参数校验设计
ERRCODE errCode = err_OK;
switch(argc)
{
case 1:
ShowHelp();
return err_OK;
case 2:
if((strcmp(argv[1], "/?") == 0)
|| (strcmp(argv[1], "/h") == 0)
|| (strcmp(argv[1], "-h") == 0))
{
ShowHelp();
errCode = err_OK;
return errCode;
}
else
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
case 4:
if(strcmp(argv[2],"to") != 0)
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
break;
default:
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
//功能代码设计
char* pszSrcFileName;
BOOL bIsFullPath;
bIsFullPath = isFullPath(argv[1]);
if (bIsFullPath)
{
pszSrcFileName = argv[1];
}
else
{
char szBuf[MAX_PATH];
pszSrcFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszSrcFileName);
pszSrcFileName = StrCat_to_FullPath(pszSrcFileName, argv[1]);
}
char* pszDstFileName;
bIsFullPath = isFullPath(argv[3]);
if (bIsFullPath)
{
pszDstFileName = argv[3];
}
else
{
char szBuf[MAX_PATH];
pszDstFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszDstFileName);
pszDstFileName = StrCat_to_FullPath(pszDstFileName, argv[3]);
}
ifstream infile(pszSrcFileName, ios::binary);
if(infile.fail() == 1)
{
cout<<"can't open file "<<pszSrcFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
ofstream outfile(pszDstFileName, ios::binary);
if(outfile.fail() == 1)
{
cout<<"can't open file "<<pszDstFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
HANDLE handleScreen;
handleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
if(!handleScreen)
{
return err_HANDLESCREEN;
}
COORD coordCursor;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
CONSOLE_CURSOR_INFO CursorInfo;
char Buffer[BUFFERSIZE];
HANDLE fSrcHandle;
WIN32_FIND_DATA FileData1;
fSrcHandle = FindFirstFile(pszSrcFileName,&FileData1);
DWORD dwSrcFileSize = FileData1.nFileSizeLow;
DWORD dwWrited = 0;
printf("\n################## File Copy Start ! ##################\n\n");
do
{
infile.read(Buffer,sizeof(char)*BUFFERSIZE);
DWORD dwReaded=infile.gcount();
outfile.write(Buffer,infile.gcount());
dwWrited += dwReaded;
ShowPer(dwWrited,dwSrcFileSize);
ShowProcess(dwWrited,dwSrcFileSize);
GetConsoleScreenBufferInfo(handleScreen, &ScreenBufferInfo);
coordCursor = ScreenBufferInfo.dwCursorPosition;
coordCursor.Y = coordCursor.Y-2;
CursorInfo.dwSize = 66;
CursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(handleScreen, &CursorInfo);
SetConsoleCursorPosition(handleScreen,coordCursor);
} while(!infile.eof());
infile.close();
outfile.close();
printf("\n\n\n\n################# File Copy Completed ! #################\n\n");
return err_OK;
}
//自定义函数行
void ShowHelp(void)
{
printf( "\n"
"Copy file. v.%s\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n"
"Copynew [/? | /h | -h]\n"
"\n"
"/? /h -h : 显示帮助信息.\n"
"srcFileName : 源文件的路径.\n"
"dstFileName : 目的文件路径.\n"
"\n",COPY_VERSION);
}
void ShowPer(DWORD dwPer, DWORD filesize)
{
DWORD show = (DWORD)(100*((float)dwPer/filesize));
printf("\r");
printf(" %d%% of the file has been copied.\n\n", show);
}
void ShowUseage(void)
{
printf( "\n"
"Copy file.v%s.\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n",COPY_VERSION);
}
void ShowProcess(DWORD dwPer, DWORD filesize)
{
printf(" $:==================================================:$");
printf("\r");
DWORD show = (DWORD)(100*((float)dwPer/filesize))/2;
printf(" $:");
for (DWORD i = 0; i<show ;i++)
{
printf(">");
}
}
BOOL isFullPath(char* pszPath)
{
DWORD dwcount;
dwcount = strlen(pszPath);
for(DWORD dwi = 0; dwi < dwcount; dwi++)
{
if(*pszPath == ':')
return TRUE;
else
pszPath ++ ;
}
return FALSE;
}
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName)
{
pszCurDirectory = strcat(pszCurDirectory,"\\");
pszCurDirectory = strcat(pszCurDirectory,pszFileName);
return pszCurDirectory;
}
以前写的程序,带console窗口进度显示的拷贝程序
你改改的用吧
个人意见,高手多多指教
#3
呵呵,大家觉得用system("Dos Command")来实现怎么样呢?
#4
C++要那么复杂吗。
ifstream ifs("xxx", ios_base::binary);
ofstream ofs("yyy", ios_base::binary);
ofs << ifs.rdbuf();
搞定。
ifstream ifs("xxx", ios_base::binary);
ofstream ofs("yyy", ios_base::binary);
ofs << ifs.rdbuf();
搞定。
#5
直接用库函数就可以把
比如copy, rename, mkdir等等
可以google一下
比如copy, rename, mkdir等等
可以google一下
#6
加了一个控制台的进度条显示,
和帮助信息
所以代码看多
和帮助信息
所以代码看多
#7
open();
read();
write();
read();
write();
#8
有这么多答案,我就不献丑了,佩服一下用console下写进度条的哥哥,真有耐性啊,不过算不得c++的东西哦,有windows api 和c函数的。
#9
就是基本的文件操作阿
#10
muroachanf(阿远之哈儿)
===========
玩写的
===========
玩写的
#11
呵呵,我最近刚好写了一个,来献献丑
#include <fstream>
using namespace std;
#define SRCFILE "F:\\C++ Project\\CopyFile\\Debug\\迅雷(Thunder).exe"// 源文件路径
#define DESFILE "F:\\C++ Project\\CopyFile\\迅雷(Thunder).exe" // 目标文件路径
#define FIXSIZE 10240
void CopyFile()
{
int nLen = GetFileLen(SRCFILE);
char szBuf[FIXSIZE] = {0};
ifstream iFile(SRCFILE, ios::binary);
ofstream oFile(DESFILE, ios::binary);
int nRead = 0;
while (nRead < nLen)
{
memset(szBuf, 0, FIXSIZE);
if ((nLen - nRead) > FIXSIZE)
{
iFile.read(szBuf, FIXSIZE);
oFile.write(szBuf, FIXSIZE);
nRead += FIXSIZE;
}
else
{
iFile.read(szBuf, (nLen - nRead));
oFile.write(szBuf, (nLen - nRead));
nRead += (nLen - nRead);
}
}
iFile.close();
oFile.close();
}
int main(int argc, char* argv[])
{
CopyFile();
}
另外,不用C++的文件流,用C的文件流速度会更快!
#include <fstream>
using namespace std;
#define SRCFILE "F:\\C++ Project\\CopyFile\\Debug\\迅雷(Thunder).exe"// 源文件路径
#define DESFILE "F:\\C++ Project\\CopyFile\\迅雷(Thunder).exe" // 目标文件路径
#define FIXSIZE 10240
void CopyFile()
{
int nLen = GetFileLen(SRCFILE);
char szBuf[FIXSIZE] = {0};
ifstream iFile(SRCFILE, ios::binary);
ofstream oFile(DESFILE, ios::binary);
int nRead = 0;
while (nRead < nLen)
{
memset(szBuf, 0, FIXSIZE);
if ((nLen - nRead) > FIXSIZE)
{
iFile.read(szBuf, FIXSIZE);
oFile.write(szBuf, FIXSIZE);
nRead += FIXSIZE;
}
else
{
iFile.read(szBuf, (nLen - nRead));
oFile.write(szBuf, (nLen - nRead));
nRead += (nLen - nRead);
}
}
iFile.close();
oFile.close();
}
int main(int argc, char* argv[])
{
CopyFile();
}
另外,不用C++的文件流,用C的文件流速度会更快!
#1
open()
read()
write()
read()
write()
#2
#include <windows.h>
#include <stdio.h>
#include <fstream.h>
#define ERRCODE DWORD
#define BUFFERSIZE 36*1024
#define COPY_VERSION "1.0"
#define err_OK 0
#define err_PARAMETER 1
#define err_OPENFILE 2
#define err_HANDLESCREEN 3
#define err_GETCURSOR 4
void ShowHelp(void);
void ShowUseage(void);
BOOL isFullPath(char* pszPath);
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName);
void ShowPer(DWORD per, DWORD filesize);
void ShowProcess(DWORD dwPer, DWORD filesize);
//useage:Copynew *** to ***
int main(int argc , char* argv[])
{
//参数校验设计
ERRCODE errCode = err_OK;
switch(argc)
{
case 1:
ShowHelp();
return err_OK;
case 2:
if((strcmp(argv[1], "/?") == 0)
|| (strcmp(argv[1], "/h") == 0)
|| (strcmp(argv[1], "-h") == 0))
{
ShowHelp();
errCode = err_OK;
return errCode;
}
else
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
case 4:
if(strcmp(argv[2],"to") != 0)
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
break;
default:
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
//功能代码设计
char* pszSrcFileName;
BOOL bIsFullPath;
bIsFullPath = isFullPath(argv[1]);
if (bIsFullPath)
{
pszSrcFileName = argv[1];
}
else
{
char szBuf[MAX_PATH];
pszSrcFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszSrcFileName);
pszSrcFileName = StrCat_to_FullPath(pszSrcFileName, argv[1]);
}
char* pszDstFileName;
bIsFullPath = isFullPath(argv[3]);
if (bIsFullPath)
{
pszDstFileName = argv[3];
}
else
{
char szBuf[MAX_PATH];
pszDstFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszDstFileName);
pszDstFileName = StrCat_to_FullPath(pszDstFileName, argv[3]);
}
ifstream infile(pszSrcFileName, ios::binary);
if(infile.fail() == 1)
{
cout<<"can't open file "<<pszSrcFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
ofstream outfile(pszDstFileName, ios::binary);
if(outfile.fail() == 1)
{
cout<<"can't open file "<<pszDstFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
HANDLE handleScreen;
handleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
if(!handleScreen)
{
return err_HANDLESCREEN;
}
COORD coordCursor;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
CONSOLE_CURSOR_INFO CursorInfo;
char Buffer[BUFFERSIZE];
HANDLE fSrcHandle;
WIN32_FIND_DATA FileData1;
fSrcHandle = FindFirstFile(pszSrcFileName,&FileData1);
DWORD dwSrcFileSize = FileData1.nFileSizeLow;
DWORD dwWrited = 0;
printf("\n################## File Copy Start ! ##################\n\n");
do
{
infile.read(Buffer,sizeof(char)*BUFFERSIZE);
DWORD dwReaded=infile.gcount();
outfile.write(Buffer,infile.gcount());
dwWrited += dwReaded;
ShowPer(dwWrited,dwSrcFileSize);
ShowProcess(dwWrited,dwSrcFileSize);
GetConsoleScreenBufferInfo(handleScreen, &ScreenBufferInfo);
coordCursor = ScreenBufferInfo.dwCursorPosition;
coordCursor.Y = coordCursor.Y-2;
CursorInfo.dwSize = 66;
CursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(handleScreen, &CursorInfo);
SetConsoleCursorPosition(handleScreen,coordCursor);
} while(!infile.eof());
infile.close();
outfile.close();
printf("\n\n\n\n################# File Copy Completed ! #################\n\n");
return err_OK;
}
//自定义函数行
void ShowHelp(void)
{
printf( "\n"
"Copy file. v.%s\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n"
"Copynew [/? | /h | -h]\n"
"\n"
"/? /h -h : 显示帮助信息.\n"
"srcFileName : 源文件的路径.\n"
"dstFileName : 目的文件路径.\n"
"\n",COPY_VERSION);
}
void ShowPer(DWORD dwPer, DWORD filesize)
{
DWORD show = (DWORD)(100*((float)dwPer/filesize));
printf("\r");
printf(" %d%% of the file has been copied.\n\n", show);
}
void ShowUseage(void)
{
printf( "\n"
"Copy file.v%s.\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n",COPY_VERSION);
}
void ShowProcess(DWORD dwPer, DWORD filesize)
{
printf(" $:==================================================:$");
printf("\r");
DWORD show = (DWORD)(100*((float)dwPer/filesize))/2;
printf(" $:");
for (DWORD i = 0; i<show ;i++)
{
printf(">");
}
}
BOOL isFullPath(char* pszPath)
{
DWORD dwcount;
dwcount = strlen(pszPath);
for(DWORD dwi = 0; dwi < dwcount; dwi++)
{
if(*pszPath == ':')
return TRUE;
else
pszPath ++ ;
}
return FALSE;
}
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName)
{
pszCurDirectory = strcat(pszCurDirectory,"\\");
pszCurDirectory = strcat(pszCurDirectory,pszFileName);
return pszCurDirectory;
}
以前写的程序,带console窗口进度显示的拷贝程序
你改改的用吧
个人意见,高手多多指教
#include <stdio.h>
#include <fstream.h>
#define ERRCODE DWORD
#define BUFFERSIZE 36*1024
#define COPY_VERSION "1.0"
#define err_OK 0
#define err_PARAMETER 1
#define err_OPENFILE 2
#define err_HANDLESCREEN 3
#define err_GETCURSOR 4
void ShowHelp(void);
void ShowUseage(void);
BOOL isFullPath(char* pszPath);
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName);
void ShowPer(DWORD per, DWORD filesize);
void ShowProcess(DWORD dwPer, DWORD filesize);
//useage:Copynew *** to ***
int main(int argc , char* argv[])
{
//参数校验设计
ERRCODE errCode = err_OK;
switch(argc)
{
case 1:
ShowHelp();
return err_OK;
case 2:
if((strcmp(argv[1], "/?") == 0)
|| (strcmp(argv[1], "/h") == 0)
|| (strcmp(argv[1], "-h") == 0))
{
ShowHelp();
errCode = err_OK;
return errCode;
}
else
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
case 4:
if(strcmp(argv[2],"to") != 0)
{
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
break;
default:
printf("Parameter error");
ShowUseage();
errCode = err_PARAMETER;
return errCode;
}
//功能代码设计
char* pszSrcFileName;
BOOL bIsFullPath;
bIsFullPath = isFullPath(argv[1]);
if (bIsFullPath)
{
pszSrcFileName = argv[1];
}
else
{
char szBuf[MAX_PATH];
pszSrcFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszSrcFileName);
pszSrcFileName = StrCat_to_FullPath(pszSrcFileName, argv[1]);
}
char* pszDstFileName;
bIsFullPath = isFullPath(argv[3]);
if (bIsFullPath)
{
pszDstFileName = argv[3];
}
else
{
char szBuf[MAX_PATH];
pszDstFileName = szBuf;
GetCurrentDirectory(MAX_PATH, pszDstFileName);
pszDstFileName = StrCat_to_FullPath(pszDstFileName, argv[3]);
}
ifstream infile(pszSrcFileName, ios::binary);
if(infile.fail() == 1)
{
cout<<"can't open file "<<pszSrcFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
ofstream outfile(pszDstFileName, ios::binary);
if(outfile.fail() == 1)
{
cout<<"can't open file "<<pszDstFileName<<endl;
errCode = err_OPENFILE;
return errCode;
}
HANDLE handleScreen;
handleScreen = GetStdHandle(STD_OUTPUT_HANDLE);
if(!handleScreen)
{
return err_HANDLESCREEN;
}
COORD coordCursor;
CONSOLE_SCREEN_BUFFER_INFO ScreenBufferInfo;
CONSOLE_CURSOR_INFO CursorInfo;
char Buffer[BUFFERSIZE];
HANDLE fSrcHandle;
WIN32_FIND_DATA FileData1;
fSrcHandle = FindFirstFile(pszSrcFileName,&FileData1);
DWORD dwSrcFileSize = FileData1.nFileSizeLow;
DWORD dwWrited = 0;
printf("\n################## File Copy Start ! ##################\n\n");
do
{
infile.read(Buffer,sizeof(char)*BUFFERSIZE);
DWORD dwReaded=infile.gcount();
outfile.write(Buffer,infile.gcount());
dwWrited += dwReaded;
ShowPer(dwWrited,dwSrcFileSize);
ShowProcess(dwWrited,dwSrcFileSize);
GetConsoleScreenBufferInfo(handleScreen, &ScreenBufferInfo);
coordCursor = ScreenBufferInfo.dwCursorPosition;
coordCursor.Y = coordCursor.Y-2;
CursorInfo.dwSize = 66;
CursorInfo.bVisible = FALSE;
SetConsoleCursorInfo(handleScreen, &CursorInfo);
SetConsoleCursorPosition(handleScreen,coordCursor);
} while(!infile.eof());
infile.close();
outfile.close();
printf("\n\n\n\n################# File Copy Completed ! #################\n\n");
return err_OK;
}
//自定义函数行
void ShowHelp(void)
{
printf( "\n"
"Copy file. v.%s\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n"
"Copynew [/? | /h | -h]\n"
"\n"
"/? /h -h : 显示帮助信息.\n"
"srcFileName : 源文件的路径.\n"
"dstFileName : 目的文件路径.\n"
"\n",COPY_VERSION);
}
void ShowPer(DWORD dwPer, DWORD filesize)
{
DWORD show = (DWORD)(100*((float)dwPer/filesize));
printf("\r");
printf(" %d%% of the file has been copied.\n\n", show);
}
void ShowUseage(void)
{
printf( "\n"
"Copy file.v%s.\n"
"Usage:\n"
" Copynew.exe srcFileName to dstFileName\n",COPY_VERSION);
}
void ShowProcess(DWORD dwPer, DWORD filesize)
{
printf(" $:==================================================:$");
printf("\r");
DWORD show = (DWORD)(100*((float)dwPer/filesize))/2;
printf(" $:");
for (DWORD i = 0; i<show ;i++)
{
printf(">");
}
}
BOOL isFullPath(char* pszPath)
{
DWORD dwcount;
dwcount = strlen(pszPath);
for(DWORD dwi = 0; dwi < dwcount; dwi++)
{
if(*pszPath == ':')
return TRUE;
else
pszPath ++ ;
}
return FALSE;
}
char* StrCat_to_FullPath(char* pszCurDirectory, char* pszFileName)
{
pszCurDirectory = strcat(pszCurDirectory,"\\");
pszCurDirectory = strcat(pszCurDirectory,pszFileName);
return pszCurDirectory;
}
以前写的程序,带console窗口进度显示的拷贝程序
你改改的用吧
个人意见,高手多多指教
#3
呵呵,大家觉得用system("Dos Command")来实现怎么样呢?
#4
C++要那么复杂吗。
ifstream ifs("xxx", ios_base::binary);
ofstream ofs("yyy", ios_base::binary);
ofs << ifs.rdbuf();
搞定。
ifstream ifs("xxx", ios_base::binary);
ofstream ofs("yyy", ios_base::binary);
ofs << ifs.rdbuf();
搞定。
#5
直接用库函数就可以把
比如copy, rename, mkdir等等
可以google一下
比如copy, rename, mkdir等等
可以google一下
#6
加了一个控制台的进度条显示,
和帮助信息
所以代码看多
和帮助信息
所以代码看多
#7
open();
read();
write();
read();
write();
#8
有这么多答案,我就不献丑了,佩服一下用console下写进度条的哥哥,真有耐性啊,不过算不得c++的东西哦,有windows api 和c函数的。
#9
就是基本的文件操作阿
#10
muroachanf(阿远之哈儿)
===========
玩写的
===========
玩写的
#11
呵呵,我最近刚好写了一个,来献献丑
#include <fstream>
using namespace std;
#define SRCFILE "F:\\C++ Project\\CopyFile\\Debug\\迅雷(Thunder).exe"// 源文件路径
#define DESFILE "F:\\C++ Project\\CopyFile\\迅雷(Thunder).exe" // 目标文件路径
#define FIXSIZE 10240
void CopyFile()
{
int nLen = GetFileLen(SRCFILE);
char szBuf[FIXSIZE] = {0};
ifstream iFile(SRCFILE, ios::binary);
ofstream oFile(DESFILE, ios::binary);
int nRead = 0;
while (nRead < nLen)
{
memset(szBuf, 0, FIXSIZE);
if ((nLen - nRead) > FIXSIZE)
{
iFile.read(szBuf, FIXSIZE);
oFile.write(szBuf, FIXSIZE);
nRead += FIXSIZE;
}
else
{
iFile.read(szBuf, (nLen - nRead));
oFile.write(szBuf, (nLen - nRead));
nRead += (nLen - nRead);
}
}
iFile.close();
oFile.close();
}
int main(int argc, char* argv[])
{
CopyFile();
}
另外,不用C++的文件流,用C的文件流速度会更快!
#include <fstream>
using namespace std;
#define SRCFILE "F:\\C++ Project\\CopyFile\\Debug\\迅雷(Thunder).exe"// 源文件路径
#define DESFILE "F:\\C++ Project\\CopyFile\\迅雷(Thunder).exe" // 目标文件路径
#define FIXSIZE 10240
void CopyFile()
{
int nLen = GetFileLen(SRCFILE);
char szBuf[FIXSIZE] = {0};
ifstream iFile(SRCFILE, ios::binary);
ofstream oFile(DESFILE, ios::binary);
int nRead = 0;
while (nRead < nLen)
{
memset(szBuf, 0, FIXSIZE);
if ((nLen - nRead) > FIXSIZE)
{
iFile.read(szBuf, FIXSIZE);
oFile.write(szBuf, FIXSIZE);
nRead += FIXSIZE;
}
else
{
iFile.read(szBuf, (nLen - nRead));
oFile.write(szBuf, (nLen - nRead));
nRead += (nLen - nRead);
}
}
iFile.close();
oFile.close();
}
int main(int argc, char* argv[])
{
CopyFile();
}
另外,不用C++的文件流,用C的文件流速度会更快!