I tried to use the system call lseek() to get back the beginning of a file or reach the end of the file.
The exact code I used is:
int location = lseek(fd, 0, SEEK_SET) //get back to the beginning
int location = lseek(fd, 0, SEEK_END) //reach to the end
However, after the file location has been reset, whenever I tried to use read(), the return value of read() is always set to -1, which means something was wrong. Furthermore, the errno message I got was Bad file descriptor. Does anyone know what should I do?
PS: I tried to close and reopen the file to help me get back to the beginning of the file and it worked. But I have no ideas on how should I get to the end of the file and read the entire file in the reverse order without using lseek().
Plus: a reproducible example would be:
#include <stdio.h>
#include <fcntl.h>
#include <string.h>
#include <unistd.h>
int main(void)
{
int fd;
char buffer[1000];
fd = creat("newFile", 0777);
memset(buffer, 'a', 500);
write(fd, buffer, 500); // fill up
int location = lseek(fd, 0, SEEK_SET); //get back to the beginning
int read_bytes = read(fd, buffer, 500);
// this should return the bytes it reads but it actually returns -1
printf("%d\n", read_bytes);
return 0;
}
The creat
function does not allow you to read from the file. It only allows you to write to it.
From creat(2)
:
creat()
A call tocreat()
is equivalent to callingopen()
withflags
equal toO_CREAT|O_WRONLY|O_TRUNC
.
The important part here is O_WRONLY
. That means "write only".
If you want to open the file (and create it) for reading and writing, then you can use open
like so:
int fd = open("newFile", O_CREAT|O_RDWR|O_TRUNC, 0777);
The important part here is O_RDWR
. That means "read and write".
If you want to have open
give an error if the file already exists, add the O_EXCL
flag; this causes -1
to be returned and errno
to be set to EEXIST
if the file already exists when you try to create it.