用C语言设计一个程序以便能快速地实现复制任意长度的文件
展开全部
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
size_t filesize;
char filer[]="1.txt";//要拷贝的文件名称(自行定义)
char filew[]="2.txt";//写入文件
char *p;
fp=fopen(filer,"rb");//使用二进制打开
if(fp==NULL)
{
printf("open %s error\n",filer);
return 1;
}
fseek(fp,0L,2);
filesize = ftell(fp); //获取文件大小
p=(char*)malloc(filesize*sizeof(char));
if(p==NULL)
{
printf("malloc error\n");
fclose(fp);
return 1;
}
fseek(fp,0L,0);
if(fread(p,sizeof(char),filesize,fp)!=filesize)//读入缓冲区
{
printf("fread error\n");
fclose(fp);
free(p);
return 1;
}
fclose(fp);
fp=fopen(filew,"wb");//二进制打开写入文件
if(fp==NULL)
{
printf("open %s error\n",filew);
free(p);
return 1;
}
if(fwrite(p,sizeof(char),filesize,fp)!=filesize)//写入
{
printf("fwrite error\n");
fclose(fp);
free(p);
return 1;
}
fclose(fp);
free(p);
printf("copy ok!\n");
return 0;
}
#include<stdlib.h>
int main()
{
FILE *fp;
size_t filesize;
char filer[]="1.txt";//要拷贝的文件名称(自行定义)
char filew[]="2.txt";//写入文件
char *p;
fp=fopen(filer,"rb");//使用二进制打开
if(fp==NULL)
{
printf("open %s error\n",filer);
return 1;
}
fseek(fp,0L,2);
filesize = ftell(fp); //获取文件大小
p=(char*)malloc(filesize*sizeof(char));
if(p==NULL)
{
printf("malloc error\n");
fclose(fp);
return 1;
}
fseek(fp,0L,0);
if(fread(p,sizeof(char),filesize,fp)!=filesize)//读入缓冲区
{
printf("fread error\n");
fclose(fp);
free(p);
return 1;
}
fclose(fp);
fp=fopen(filew,"wb");//二进制打开写入文件
if(fp==NULL)
{
printf("open %s error\n",filew);
free(p);
return 1;
}
if(fwrite(p,sizeof(char),filesize,fp)!=filesize)//写入
{
printf("fwrite error\n");
fclose(fp);
free(p);
return 1;
}
fclose(fp);
free(p);
printf("copy ok!\n");
return 0;
}
展开全部
BOOL CopyFile来拷贝就行了;
BOOL CopyFile(
LPCTSTR lpExistingFileName,// 指向现有文件的指针。
LPCTSTR lpNewFileName,//向目标文件的指针
BOOL bFailIfExists//来说明目标是否存在,不存在就是FALSE,存在就是TRUE
);
调用:
BOOL flag=CopyFile("F:\\2.txt","F:\\\\1.txt",TRUE);
下面是msdn里面的 复制某目录里面所有txt文件的(当然你可以选择任何格式文件) 的代码
#include <windows.h>
#include <stdio.h>
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
printf("Couldn't create new directory.");
return;
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No .TXT files found.");
return;
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Couldn't copy file.");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}
// Close the search handle.
FindClose(hSearch
BOOL CopyFile(
LPCTSTR lpExistingFileName,// 指向现有文件的指针。
LPCTSTR lpNewFileName,//向目标文件的指针
BOOL bFailIfExists//来说明目标是否存在,不存在就是FALSE,存在就是TRUE
);
调用:
BOOL flag=CopyFile("F:\\2.txt","F:\\\\1.txt",TRUE);
下面是msdn里面的 复制某目录里面所有txt文件的(当然你可以选择任何格式文件) 的代码
#include <windows.h>
#include <stdio.h>
WIN32_FIND_DATA FileData;
HANDLE hSearch;
DWORD dwAttrs;
char szDirPath[] = "c:\\TEXTRO\\";
char szNewPath[MAX_PATH];
char szHome[MAX_PATH];
BOOL fFinished = FALSE;
// Create a new directory.
if (!CreateDirectory(szDirPath, NULL))
{
printf("Couldn't create new directory.");
return;
}
// Start searching for .TXT files in the current directory.
hSearch = FindFirstFile("*.txt", &FileData);
if (hSearch == INVALID_HANDLE_VALUE)
{
printf("No .TXT files found.");
return;
}
// Copy each .TXT file to the new directory
// and change it to read only, if not already.
while (!fFinished)
{
lstrcpy(szNewPath, szDirPath);
lstrcat(szNewPath, FileData.cFileName);
if (CopyFile(FileData.cFileName, szNewPath, FALSE))
{
dwAttrs = GetFileAttributes(FileData.cFileName);
if (!(dwAttrs & FILE_ATTRIBUTE_READONLY))
{
SetFileAttributes(szNewPath,
dwAttrs | FILE_ATTRIBUTE_READONLY);
}
}
else
{
printf("Couldn't copy file.");
return;
}
if (!FindNextFile(hSearch, &FileData))
{
if (GetLastError() == ERROR_NO_MORE_FILES)
{
MessageBox(hwnd, "No more .TXT files.",
"Search completed.", MB_OK);
fFinished = TRUE;
}
else
{
printf("Couldn't find next file.");
return;
}
}
}
// Close the search handle.
FindClose(hSearch
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我下载好了。等等一起打印出来
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询