标准I/O文件复制,文件I/O文件复制

来源:互联网 发布:linux informix 安装 编辑:程序博客网 时间:2024/06/05 02:23
#include <stdio.h>
//#include <sys/types.h>
//#include <sys/stat.h>
//#include <fcntl.h>
#include <errno.h>
#include <string.h>
//#include <unistd.h>
#define N 32




/*int main()
{
int fd,fd1,n = 1,sum = 0;
char buf[8];
if((fd = open("test1.c",O_CREAT|O_RDONLY,0666)) < 0)
perror("test1.c");
if((fd1 = open("test2.c",O_TRUNC|O_CREAT|O_RDWR,0666)) < 0)
perror("open test2.c");
while((n = read(fd,buf,8)) > 0)
{
lseek(fd,,SEEK_CUR);
write(fd1,buf,n);
}
close(fd);
close(fd1);
return 0;
}*/
int main(int argc,char *argv[])
{
FILE *fp1,*fp2;
char buf[128];
int n;
/*if(argc < 3)
{
fprintf(stderr,"number is %s\n",strerror(errno));
return -1;
}*/
if((fp1 = fopen("test1.c","r+")) == NULL)
{
fprintf(stdout,"test1 is %s\n",strerror(errno));
return -1;
}
if((fp2 = fopen("test2.c","w+")) == NULL)
{
fprintf(stdout," test2.c is %s\n",strerror(errno));
return -1;
}
int i = 0;
while(fgets(buf,128,fp1)!=NULL)
// fgets(buf,128,fp1);
{
fseek(fp1,1,SEEK_CUR);
printf("%lu\n",ftell(fp1));
fputs(buf,fp2); 
}
fclose(fp1);
fclose(fp2);
return 0;
}
0 0