I'm just learning the unix system calls and have used lseek a few times and never had a problem with it. It's a pretty straight forward system call but this one is getting me. I'm getting the error "problem with lseek in lchars: 1 : Illegal seek"
I've used the system call in this exact way but am getting this error for one reason or another. Probably overlooking something.
Code:
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <ctype.h>
int
main(int argc, char **argv)
{
int fd1, fd2;
int i;
char buf[1];
if (fd1 = open(argv[1], O_RDONLY) == -1) {
perror("problem with open in lchars: 1");
exit(-1);
}
if (fd2 = open(argv[2], O_CREAT|O_WRONLY, 0664) == -1) {
perror("problem with open in lchars: 2");
exit(-1);
}
i = 1;
if (lseek(fd1, (off_t)-i, SEEK_END) == -1) {
perror("problem with lseek in lchars: 1");
exit(-1);
}
/* strip spaces at end of file */
read(fd1, buf, sizeof(buf));
while (isspace(buf[i])) {
++i;
lseek(fd1, (off_t)-i, SEEK_END);
read(fd1, buf, sizeof(buf));
}
for (i; i < (i + 10); ++i) {
write(fd2, buf, sizeof(buf));
lseek(fd1, (off_t)-i, SEEK_END);
read(fd1, buf, sizeof(buf));
}
return 0;
}