私がやったこと:ファイルの内容を逆の順序でコピーします。私ができないこと: コンテンツを順方向にコピーします。
私はウェブで調査を行い、lseek()
この議論があることを発見しました..
lseek(file_descriptor,offset,whence);
逆読みの場合、ロジックは簡単です。そして私のコードは次のとおりです:
#include<fcntl.h>
#include<unistd.h>
#include<stdio.h>
#include<stdlib.h>
void main(int argc,char *argv[])
{
int fd,fd1,count=0;
char ch='y',buffer;
if(argc>3)
{
printf("\nSyntax Error!<try> ./cp source_name destination_name\n");
exit(0);
}
fd=open(argv[1],O_RDWR);
if(fd==-1)
{
printf("\nCan not open source file .. :(\n");exit(0);
}
fd1=open(argv[2],O_RDWR);
if(fd1=!-1)
{
printf("\nDestination file exist , OverWrite (Y/N) :");
scanf("%s",&ch);
}
if(ch=='y'||ch=='Y')
fd1=creat(argv[2],O_RDWR|0666);
else
exit(0);
while(lseek(fd,count,2)!=-1)
{
count--;
read(fd,&buffer,sizeof(char));
write(fd1,&buffer,sizeof(char));
}
}
ファイルを順方向にコピーするためにできると思った変更。
count=0;
lseek(fd,count,0)!=-1
but this is taking the program in infinite loop . need help .