c语言拷贝文件程序

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 1024*1024*10

int main()
{
	FILE *fp_read, *fp_write;
	fp_read = fopen("E:\\CentOS-6.5\\CentOS-6.5-i386-bin-DVD1.iso","rb");
	fp_write = fopen("D:\\CentOS-6.5-i386-bin-DVD1.iso","wb");
	char *buf;
	int read_n, write_n;
	buf = (char*)calloc(1,SIZE);
	if (fp_read == NULL || fp_write == NULL){
		perror("File open fail.\n");
	}
	printf("开始拷贝 %s:\n","pycharm-professional-3.1.3.exe");
	while (memset(buf, 0, SIZE), (read_n = fread(buf, 1, SIZE, fp_read)) > 0){
		write_n = fwrite(buf, 1, read_n, fp_write);
		if (write_n != read_n){
			printf("write error\n");
			system("pause");
			exit(-1);
		}
	}
	printf("%s 拷贝成功.\n","pycharm-professional-3.1.3.exe");
	fclose(fp_read);
	fclose(fp_write);
}


Windows下使用C语言实现大文件拷贝可以使用WinAPI中的文件操作函数。下面是一个简单的示例代码: ```c #include <stdio.h> #include <windows.h> #define BUFFER_SIZE 1024 * 1024 // 缓冲区大小,这里设为1MB int main(int argc, char* argv[]) { if (argc != 3) { printf("Usage: %s source_file target_file\n", argv[0]); return 1; } HANDLE hSrcFile = CreateFile(argv[1], GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); if (hSrcFile == INVALID_HANDLE_VALUE) { printf("Failed to open source file.\n"); return 1; } HANDLE hDstFile = CreateFile(argv[2], GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); if (hDstFile == INVALID_HANDLE_VALUE) { printf("Failed to create target file.\n"); CloseHandle(hSrcFile); return 1; } DWORD dwBytesRead, dwBytesWritten; char buffer[BUFFER_SIZE]; while (ReadFile(hSrcFile, buffer, BUFFER_SIZE, &dwBytesRead, NULL) && dwBytesRead > 0) { if (!WriteFile(hDstFile, buffer, dwBytesRead, &dwBytesWritten, NULL) || dwBytesWritten != dwBytesRead) { printf("Failed to write to target file.\n"); CloseHandle(hSrcFile); CloseHandle(hDstFile); return 1; } } CloseHandle(hSrcFile); CloseHandle(hDstFile); printf("File copy completed.\n"); return 0; } ``` 该程序会从命令行参数中读取源文件名和目标文件名,并使用CreateFile函数打开源文件和创建目标文件。然后,使用ReadFile和WriteFile函数进行文件的读写操作,读取BUFFER_SIZE大小的数据并写入目标文件,直到读取完整个源文件。最后调用CloseHandle函数关闭文件句柄。注意在进行文件操作时需要进行错误检查,以确保文件操作的正确性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值