使用fread和fwrite拷贝文件
#include<stdio.h>#include<stdlib.h>#include<sys/types.h>#defineREAD_BUFF100intmain(in...
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define READ_BUFF 100
int main (int argc,char *argv[])
{
if(argc != 3)
{
printf("Input like this:\n");
printf("./readwrite readfile writefile!\n");
}
char buff[READ_BUFF]={0};
FILE *fp_read,*fp_write;
if((fp_read=fopen(argv[1],"rb"))==NULL)
{
printf("Cann't open %s !",argv[1]);
return 1;
}
if((fp_write=fopen(argv[2],"wb"))==NULL)
{
printf("Cann't open %s !",argv[2]);
return 1;
}
while((fread(buff,READ_BUFF,1,fp_read))>0)
fwrite(buff,nread,1,fp_write);
if(buff[READ_BUFF]!=NULL)
fwrite(buff,sizeof(buff),1,fp_write);
fclose(fp_read);
fclose(fp_write);
return 0;
}
出现问题如下:
1、如果read文件大小是9088,则写文件大小是9100.两者内容是一样的。
2、如果read文件大小是9000,则写文件大小是9100.多出了后100个字节。
请帮忙修改下程序以完成拷贝任务(就如同cp命令,大小和内容都是一样的)。 展开
#include <stdlib.h>
#include <sys/types.h>
#define READ_BUFF 100
int main (int argc,char *argv[])
{
if(argc != 3)
{
printf("Input like this:\n");
printf("./readwrite readfile writefile!\n");
}
char buff[READ_BUFF]={0};
FILE *fp_read,*fp_write;
if((fp_read=fopen(argv[1],"rb"))==NULL)
{
printf("Cann't open %s !",argv[1]);
return 1;
}
if((fp_write=fopen(argv[2],"wb"))==NULL)
{
printf("Cann't open %s !",argv[2]);
return 1;
}
while((fread(buff,READ_BUFF,1,fp_read))>0)
fwrite(buff,nread,1,fp_write);
if(buff[READ_BUFF]!=NULL)
fwrite(buff,sizeof(buff),1,fp_write);
fclose(fp_read);
fclose(fp_write);
return 0;
}
出现问题如下:
1、如果read文件大小是9088,则写文件大小是9100.两者内容是一样的。
2、如果read文件大小是9000,则写文件大小是9100.多出了后100个字节。
请帮忙修改下程序以完成拷贝任务(就如同cp命令,大小和内容都是一样的)。 展开
展开全部
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#define READ_BUFF 100
int main (int argc,char *argv[])
{
if(argc != 3)
{
printf("Input like this:\n");
printf("./readwrite readfile writefile!\n");
}
int nread = 0;
char buff[READ_BUFF]={0};
FILE *fp_read,*fp_write;
if((fp_read=fopen(argv[1],"rb"))==NULL)
{
printf("Cann't open %s !",argv[1]);
return 1;
}
if((fp_write=fopen(argv[2],"wb"))==NULL)
{
printf("Cann't open %s !",argv[2]);
return 1;
}
while((nread=fread(buff,sizeof(char),READ_BUFF,fp_read))>0)
fwrite(buff,sizeof(char),nread,fp_write);
fclose(fp_read);
fclose(fp_write);
return 0;
}
#include <stdlib.h>
#include <sys/types.h>
#define READ_BUFF 100
int main (int argc,char *argv[])
{
if(argc != 3)
{
printf("Input like this:\n");
printf("./readwrite readfile writefile!\n");
}
int nread = 0;
char buff[READ_BUFF]={0};
FILE *fp_read,*fp_write;
if((fp_read=fopen(argv[1],"rb"))==NULL)
{
printf("Cann't open %s !",argv[1]);
return 1;
}
if((fp_write=fopen(argv[2],"wb"))==NULL)
{
printf("Cann't open %s !",argv[2]);
return 1;
}
while((nread=fread(buff,sizeof(char),READ_BUFF,fp_read))>0)
fwrite(buff,sizeof(char),nread,fp_write);
fclose(fp_read);
fclose(fp_write);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询