编写一个文件以.c另一个文件 [英] Writing a file to another file in .c
本文介绍了编写一个文件以.c另一个文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个code读取文件,然后复制它的内容到另一个文件。我需要它来复制只能每20符号,然后跳过10个符号,然后再20符号等。
我必须使用lseek的()函数,但我不知道如何把所有这一切在循环做。
主(ARGC,ARGV)
INT ARGC;
的char * ARGV [];
{
INT FD1,FD2;
INT为nbytes,模式;
烧焦的buf [BUFSIZ];
如果(argc个3;){
fprintf中(标准错误,%S:nepareizs kopesanas parametrs \\ n,argv的[0]);
出口(1);
}
如果((FD1 =打开(的argv [1],O_RDONLY))小于0){
fprintf中(标准错误,Nevar atvert failu%S \\ N的argv [1]);
出口(1);
}
如果((FD2 =打开(的argv [2],O_WRONLY | O_CREAT,模式))小于0){
fprintf中(标准错误,Nevar izveidot jaunu failu%S \\ N的argv [2]);
出口(1);
} 为nbytes = lseek的(FD1,10,0)
而((为nbytes =读(FD1,BUF,BUFSIZ))0){
如果(写(FD2,BUF,为nbytes)小于0){
fprintf中(标准错误,непонимаю\\ n);
打破; } } 如果(为nbytes℃,)
fprintf中(标准错误,непонимаю\\ n);
关闭(FD1);
接近(FD2);
出口(0);
}
解决方案
替换
的nbytes = lseek的(FD1,10,0)
而((为nbytes =读(FD1,BUF,BUFSIZ))0){
如果(写(FD2,BUF,为nbytes)小于0){
fprintf中(标准错误,непонимаю\\ n);
打破; }}
与
,而((为nbytes =读(FD1,BUF,BUFSIZ))0){
/ *首先你写你的20个字节* /
如果(写(FD2,BUF,为nbytes)小于0){
fprintf中(标准错误,непонимаю\\ n);
打破; }
/ *然后再移动10个字节* /
如果(了lseek(FD1,10,0)℃,){
/ *休息,如果这是EOF * /
打破;
}}
如果BUFSIZ为20。
,或者简单地说,与BUFSIZ = 30:
,而((为nbytes =读(FD1,BUF,BUFSIZ))0){
/ *你只写了前20个读取的字节,或为nbytes
如果在缓冲区中没有多少字节* /
如果(?写(FD2,BUF,为nbytes< 20为nbytes:20)℃下){
fprintf中(标准错误,непонимаю\\ n);
打破;
}
}
I have a code that reads file and then copies its content to the another file. I need to make it to copy only every 20 symbols and then skip 10 symbols and then again 20 symbols and so on.
I have to use lseek() function but I don't know how to put all of that in the cycle to do it.
main (argc, argv)
int argc;
char *argv[];
{
int fd1, fd2;
int nbytes, mode;
char buf[BUFSIZ];
if(argc<3){
fprintf(stderr, "%s: nepareizs kopesanas parametrs\n", argv[0]);
exit(1);
}
if((fd1 = open(argv[1], O_RDONLY)) < 0){
fprintf(stderr, "Nevar atvert failu %s\n", argv[1]);
exit(1);
}
if((fd2 = open(argv[2], O_WRONLY | O_CREAT, mode))<0){
fprintf(stderr, "Nevar izveidot jaunu failu %s\n", argv[2]);
exit(1);
}
nbytes = lseek(fd1, 10, 0)
while((nbytes = read(fd1, buf, BUFSIZ))>0){
if(write(fd2, buf, nbytes) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
}
if (nbytes<0)
fprintf(stderr, "не понимаю\n");
close(fd1);
close(fd2);
exit(0);
}
解决方案
Replace
nbytes = lseek(fd1, 10, 0)
while((nbytes = read(fd1, buf, BUFSIZ))>0){
if(write(fd2, buf, nbytes) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
}
with
while((nbytes = read(fd1, buf, BUFSIZ))>0){
/* first you write your 20 bytes */
if(write(fd2, buf, nbytes) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
/* and then you move 10 bytes further */
if (lseek(fd1, 10, 0) < 0){
/* break if this is the eof */
break;
}
}
if BUFSIZ is 20.
or, simplest, with BUFSIZ = 30 :
while((nbytes = read(fd1, buf, BUFSIZ))>0){
/* you only write the first 20 read bytes, or nbytes
if there are no much bytes in the buffer */
if(write(fd2, buf, nbytes < 20 ? nbytes : 20) < 0){
fprintf(stderr, "не понимаю\n");
break;
}
}
这篇关于编写一个文件以.c另一个文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文