c语言高手进 copy file
Writeafilecopyprogramthatcopiesanexistingfileintoanotherfile.Theprogramshouldasktheus...
Write a file copy program that copies an existing file into another file. The program should ask the user to enter the source file name and destination file name. The file copy should be performed in binary mode. Use fseek() to move within the source file and ftell() to determine the size of the file (if needed). Read the data from the source file and write the values read in the destination file in a loop. Read and write data in blocks of 1024 bytes.
不要看他们的程序。。。是错的。。。希望高手自己做。。。好的可以追加分! 展开
不要看他们的程序。。。是错的。。。希望高手自己做。。。好的可以追加分! 展开
4个回答
展开全部
这是我刚刚自己写的,在vc6.0下编译通过,生成可执行文件fileCopy,直接在cmd命令行下可以运行!
在C盘目录下建一个a.txt,然后执行: fileCopy C:\a.txt C:\b.txt 即可!
源代码:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 100 //最长文件名
#define BLOCK 1024 //每一块大小为1024字节
int main(int argc, char* argv[])
{
char srcFile[NAME_LEN], desFile[NAME_LEN];
//首先判断有没有输入源文件名、目标文件名
//没有的话,提示用户输入文件名
if(argc<2)
{
printf("Input the source file name:");
scanf("%s",srcFile);
printf("Input the destination file name:");
scanf("%s",desFile);
}
//只输入源文件名,提示用户输入目标文件名
else if(argc<3)
{
strncpy(srcFile, argv[1], NAME_LEN);
printf("Input the destination file name:");
scanf("%s",desFile);
}
else
{
strncpy(srcFile, argv[1], NAME_LEN);
strncpy(desFile, argv[2], NAME_LEN);
}
FILE *fpSrc, *fpDes;
long fileSize;
char buf[BLOCK];
//打开源文件,不存在时提示用户,程序退出
if( (fpSrc = fopen(srcFile, "rb"))==NULL)
{
printf("The source file %s was not found\n", srcFile);
return 1;
}
if( (fpDes = fopen(desFile, "wb"))==NULL)
{
printf("Copying file failed\n");
return 1;
}
fseek(fpSrc, 0L, SEEK_END);
fileSize=ftell(fpSrc);
rewind(fpSrc);
//每次读取一个BLOCK,即1024字节
while(fileSize >= BLOCK)
{
fread(buf, BLOCK, 1, fpSrc);
fwrite(buf,BLOCK, 1, fpDes);
fileSize -= BLOCK;
}
//最后可能还剩不够一个BLOCK,不要忘记了哦!
if(fileSize)
{
fread(buf, fileSize, 1, fpSrc);
fwrite(buf,fileSize, 1, fpDes);
}
printf("File copying finished!\n");
//关闭文件
fclose(fpSrc);
fclose(fpDes);
return 0;
}
在C盘目录下建一个a.txt,然后执行: fileCopy C:\a.txt C:\b.txt 即可!
源代码:
#include <stdio.h>
#include <string.h>
#define NAME_LEN 100 //最长文件名
#define BLOCK 1024 //每一块大小为1024字节
int main(int argc, char* argv[])
{
char srcFile[NAME_LEN], desFile[NAME_LEN];
//首先判断有没有输入源文件名、目标文件名
//没有的话,提示用户输入文件名
if(argc<2)
{
printf("Input the source file name:");
scanf("%s",srcFile);
printf("Input the destination file name:");
scanf("%s",desFile);
}
//只输入源文件名,提示用户输入目标文件名
else if(argc<3)
{
strncpy(srcFile, argv[1], NAME_LEN);
printf("Input the destination file name:");
scanf("%s",desFile);
}
else
{
strncpy(srcFile, argv[1], NAME_LEN);
strncpy(desFile, argv[2], NAME_LEN);
}
FILE *fpSrc, *fpDes;
long fileSize;
char buf[BLOCK];
//打开源文件,不存在时提示用户,程序退出
if( (fpSrc = fopen(srcFile, "rb"))==NULL)
{
printf("The source file %s was not found\n", srcFile);
return 1;
}
if( (fpDes = fopen(desFile, "wb"))==NULL)
{
printf("Copying file failed\n");
return 1;
}
fseek(fpSrc, 0L, SEEK_END);
fileSize=ftell(fpSrc);
rewind(fpSrc);
//每次读取一个BLOCK,即1024字节
while(fileSize >= BLOCK)
{
fread(buf, BLOCK, 1, fpSrc);
fwrite(buf,BLOCK, 1, fpDes);
fileSize -= BLOCK;
}
//最后可能还剩不够一个BLOCK,不要忘记了哦!
if(fileSize)
{
fread(buf, fileSize, 1, fpSrc);
fwrite(buf,fileSize, 1, fpDes);
}
printf("File copying finished!\n");
//关闭文件
fclose(fpSrc);
fclose(fpDes);
return 0;
}
展开全部
写一个复制文件的程序,能复制一个存在的文件到另一个。程序让用户输入源文件名和目标文件名,要用二进制模式复制文件。用fseek()函数和ftell() 函数得到源文件长度(如有必要),使用循环且用固定大小1024字节的块来读写.
main(){
FILE *fps,*fpd;
char str[1024]={0};
long len;
fps=fopen("s.txt","rb");
fpd=fopen("d.txt","wb");
fseek(fps,0l,SEEK_END);
len=ftell(fps);
rewind(fps);
while(1){
fread(str,1024L,fps);
if(feof(fps))
break;
fwrite(str,1024L,fpd);
}
printf("\nOk");
getch();
}
main(){
FILE *fps,*fpd;
char str[1024]={0};
long len;
fps=fopen("s.txt","rb");
fpd=fopen("d.txt","wb");
fseek(fps,0l,SEEK_END);
len=ftell(fps);
rewind(fps);
while(1){
fread(str,1024L,fps);
if(feof(fps))
break;
fwrite(str,1024L,fpd);
}
printf("\nOk");
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Write a file copy program that copies an existing file into another file. The program should ask the user to enter the source file name and destination file name. The file copy should be performed in binary mode. Use fseek() to move within the source file and ftell() to determine the size of the file (if needed). Read the data from the source file and write the values read in the destination file in a loop. Read and write data in blocks of 1024 bytes.
写一个复制文件的程序,能复制一个存在的文件到另一个。程序让用户输入源文件名和目标文件名,要用二进制模式复制文件。用fseek()函数和ftell() 函数得到源文件长度(如有必要),使用循环且用固定大小1024字节的块来读写.
main(){
FILE *fps,*fpd;
char str[1024]={0};
long len;
fps=fopen("s.txt","rb");
fpd=fopen("d.txt","wb");
fseek(fps,0l,SEEK_END);
len=ftell(fps);
rewind(fps);
while(1){
fread(str,1024L,fps);
if(feof(fps))
break;
fwrite(str,1024L,fpd);
}
printf("\nOk");
getch();
}
写一个复制文件的程序,能复制一个存在的文件到另一个。程序让用户输入源文件名和目标文件名,要用二进制模式复制文件。用fseek()函数和ftell() 函数得到源文件长度(如有必要),使用循环且用固定大小1024字节的块来读写.
main(){
FILE *fps,*fpd;
char str[1024]={0};
long len;
fps=fopen("s.txt","rb");
fpd=fopen("d.txt","wb");
fseek(fps,0l,SEEK_END);
len=ftell(fps);
rewind(fps);
while(1){
fread(str,1024L,fps);
if(feof(fps))
break;
fwrite(str,1024L,fpd);
}
printf("\nOk");
getch();
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
头文件没有,入口函数也不标准。。。
#include <stdio.h>
int main(int arga,char** argc){
#include <stdio.h>
int main(int arga,char** argc){
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询