linux下使用fread和fwrite的问题:我想实现一个文件到另一个文件的简单复制,总是编译出错.
#include<stdio.h>#include<stdlib.h>#defineBUFSIZE1024voidcp(FILE*source,FILE*dest);in...
#include <stdio.h>
#include <stdlib.h>
#define BUFSIZE 1024
void cp(FILE *source,FILE *dest);
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
if(argc!=3)
{
fprintf(stderr,"Usage:%s filename1 filename2\n",argv[0]);
exit(EXIT_FAILURE);
}
if((fp1=fopen(argv[1],"rb"))==NULL)
{
fprintf(stderr,"Cann't open file %s.\n",argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fp1,NULL,_IOFBF,BUFSIZE)!=0)
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
}
if((fp2=fopen(argv[2],"wb"))==NULL)
{
fprintf(stderr,"Cann't open file %s.\n",argv[2]);
exit(EXIT_FAILURE);
}
if(setvbuf(fp2,NULL,_IOFBF,BUFSIZE)!=0)
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
{
cp(fp1,fp2);
if(ferror(fp1)!=0)
{
fprintf(stderr,"Error in reading %s.\n",argv[1]);
//exit(EXIT_FAILURE);
}
if(ferror(fp2)!=0)
{
fprintf(stderr,"Error in writing %s.\n",argv[2]);
// exit(EXIT_FAILURE);
}
if(fclose(fp1)!=0)
{
fprintf(stderr,"Error in closing %s.\n",argv[1]);
exit(EXIT_FAILURE);
}
if(fclose(fp2)!=0)
{
fprintf(stderr,"Error in closing %s.\n",argv[2]);
exit(EXIT_FAILURE);
}
return 0;
}
void cp(FILE *source,FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes=fread(temp,sizeof(char),BUFSIZE,source))>0)
fwrite(temp,sizeof(char),bytes,dest);
}
gcc 编译总是出错:parse error at end of input。 展开
#include <stdlib.h>
#define BUFSIZE 1024
void cp(FILE *source,FILE *dest);
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
if(argc!=3)
{
fprintf(stderr,"Usage:%s filename1 filename2\n",argv[0]);
exit(EXIT_FAILURE);
}
if((fp1=fopen(argv[1],"rb"))==NULL)
{
fprintf(stderr,"Cann't open file %s.\n",argv[1]);
exit(EXIT_FAILURE);
}
if(setvbuf(fp1,NULL,_IOFBF,BUFSIZE)!=0)
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
}
if((fp2=fopen(argv[2],"wb"))==NULL)
{
fprintf(stderr,"Cann't open file %s.\n",argv[2]);
exit(EXIT_FAILURE);
}
if(setvbuf(fp2,NULL,_IOFBF,BUFSIZE)!=0)
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
{
cp(fp1,fp2);
if(ferror(fp1)!=0)
{
fprintf(stderr,"Error in reading %s.\n",argv[1]);
//exit(EXIT_FAILURE);
}
if(ferror(fp2)!=0)
{
fprintf(stderr,"Error in writing %s.\n",argv[2]);
// exit(EXIT_FAILURE);
}
if(fclose(fp1)!=0)
{
fprintf(stderr,"Error in closing %s.\n",argv[1]);
exit(EXIT_FAILURE);
}
if(fclose(fp2)!=0)
{
fprintf(stderr,"Error in closing %s.\n",argv[2]);
exit(EXIT_FAILURE);
}
return 0;
}
void cp(FILE *source,FILE *dest)
{
size_t bytes;
static char temp[BUFSIZE];
while((bytes=fread(temp,sizeof(char),BUFSIZE,source))>0)
fwrite(temp,sizeof(char),bytes,dest);
}
gcc 编译总是出错:parse error at end of input。 展开
3个回答
展开全部
最好用fgets()来读取一行,该函数自动读取一行。
写的时候你就可以自己随意定啦!你在buf后面加个换行符'\n'就行了,fwrite()就会自动换行了。
比如:
FILE* fp = fopen("c:\\test.txt","rw"); //打开文件
if( fp == NULL )
return;
char achBuf[256] = ;
fgets( achBuf,fp ); //读取一行,直到遇到换行符结束
memset( achBuf,0,256 );
strcpy(achBuf,"Hello,world!\n"); //内容随意,别忘了加个换行符
fwrite( achBuf,1,strlen(achBuf)+1,fp ); //写入一行
fclose(fp); //关闭文件
写的时候你就可以自己随意定啦!你在buf后面加个换行符'\n'就行了,fwrite()就会自动换行了。
比如:
FILE* fp = fopen("c:\\test.txt","rw"); //打开文件
if( fp == NULL )
return;
char achBuf[256] = ;
fgets( achBuf,fp ); //读取一行,直到遇到换行符结束
memset( achBuf,0,256 );
strcpy(achBuf,"Hello,world!\n"); //内容随意,别忘了加个换行符
fwrite( achBuf,1,strlen(achBuf)+1,fp ); //写入一行
fclose(fp); //关闭文件
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
if(setvbuf(fp2,NULL,_IOFBF,BUFSIZE)!=0)
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
{
cp(fp1,fp2);
看到没有,这么明显的错误,一个括号方向打反了,改成 }运行ok。
{
fprintf(stderr,"Error in creat buffers.\n");
exit(EXIT_FAILURE);
{
cp(fp1,fp2);
看到没有,这么明显的错误,一个括号方向打反了,改成 }运行ok。
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
﹌
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询