I'm trying to set the current position to a location well beyond the end of the file.
However, when I input a string such as "testing 1 2 3.." at the end of the file with offset eg 1000 bytes, I can no longer open the file. It says
Code:
gedit has not been able to detect the character coding.
Please check that you are not trying to open a binary file.
Select a character coding from the menu and try again.
Also, is there a command I can put inside my code that actually reads the file and displays it on the screen?
Seems like nread just returns a ssize_t value.
Code
Code:
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
int main()
{
int fd;
ssize_t nread,nwrite,nbytes;
off_t newpos;
char buffer[1024];
/*open file for reading*/
fd = open("lseek.txt",O_RDWR);
if (fd == -1)
{
printf("Error! Could not open file\n");
exit(1);
}
newpos = lseek(fd,20000, SEEK_END);
nread = read(fd, buffer, 20);
printf("End of file: %s\n", buffer);
strcpy(buffer,"testing 1 2 3...");
nbytes = strlen(buffer);
nwrite = write(fd,buffer,nbytes);
printf("End of file: %s\n", buffer);
close(fd);
}