I can't understand why this piece of code does not work. Read() returns -1. I opened file like this:
int descr = open(filepath, O_RDWR);
then I tried this:
void* bufi = malloc(blockSize);
void* bufj = malloc(blockSize);
//Loading to buffs
descr = lseek(descr, blockSize*i, SEEK_SET);
printf("%zd\n", read(descr, bufi, blockSize));
descr = lseek(descr, blockSize*j, SEEK_SET);
read(descr, bufj, blockSize);
//Writing from bufs to apropriate places in file
descr = lseek(descr, blockSize*j, SEEK_SET);
write(descr, bufi, blockSize);
descr = lseek(descr, blockSize*i, SEEK_SET);
write(descr, bufj, blockSize);
File I tried to open was treated with "chmod 777" in case that was a problem. Thanks in advance.
int descr = open(filepath, O_RDWR);
descr = lseek(descr, blockSize*i, SEEK_SET);
printf("%zd\n", read(descr, bufi, blockSize));
By the time you get to read
, you've changed the value of descr
to be whatever lseek
returned, which is very unlikely to be a valid file descriptor. Don't use descr
to hold the value returned by lseek
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.