Questions tagged [lseek]

A linux C API function that repositions the offset of the open file associated with the file descriptor to the argument offset according to the directive.

81 questions
2
votes
3 answers

SEEK_HOLE and SEEK_DATA not working in Ubuntu 12.04.2 LTS

When compiling I get the error: cc holetest.c -o holetest holetest.c: In function ‘test_seek’: holetest.c:48:19: error: ‘SEEK_HOLE’ undeclared (first use in this function) holetest.c:48:19: note: each undeclared identifier is reported only…
fadedbee
  • 42,671
  • 44
  • 178
  • 308
1
vote
1 answer

lseek SEEK_DATA appears to be not working

Running on Tumbleweed with kernel 6.2.12-1-default Using the _GNU_SOURCE define should allow the use of SEEK_DATA as the whence value for lseek. According to the man page this should work for a range of file systems. I have tested this code on btrfs…
Rashid
  • 41
  • 2
1
vote
0 answers

C POSIX function lseek returns -1 but errno is not set

I have encountered a situation where after calling lseek, I would get -1 as a return value but errno would remain unset. From what I understand by reading the documentation for lseek, -1 return value would indicate an error. Upon successful…
Long Claw
  • 107
  • 2
  • 6
1
vote
1 answer

Unexpected behaviour of lseek

I am using lseek just to find the size of a file and lseek returns less bytes than the file's actual size. I think the code is right and I can't explain why that happens. When i ran the program for the first time it worked well. Then I added some…
1
vote
1 answer

C - Wrong read after lseek with define expression

I have a binary file from which I need to read timestamps. In particular, I'm trying to read the last record. Every record is 24 bytes long, the timestamp is in the first 8 bytes. Here's the code I wrote: #define IDX_RS_LEN (unsigned…
bui3
  • 123
  • 11
1
vote
1 answer

C lseek misbehaving

Im trying to write a program that emulates the tail behavior in C. When searching the file for '/n' chars, im getting a strange behavior from lseek. Here is the relevant piece of code: FILE *myfile = fopen(argv[i], "r"); if (myfile == NULL)…
Rob
  • 15
  • 6
1
vote
0 answers

What's linux kernel equivalent of lseek?

I'm writing a linux device driver with linux kernel modules, and I want handle situation when user writes after size of the file. I saw there is vfs_lseek(), but it takes an fd, insted I have only struct* file pointer. I could use an equivalent of…
Claudio Santoro
  • 69
  • 2
  • 11
1
vote
1 answer

Is there a way to find the position of a special character '\n' in a file in C?

I want to find the position of a '\n' in a file and print out the remaining characters after \n I've tried to use lseek to find the number of times '\n' occurred but I cannot seem to find the position at which the special character '\n' occurs…
alfie
  • 157
  • 10
1
vote
0 answers

difference between reading the /proc/stat files via syscall fucntion read () or via glibc function fread()

Program reading /proc/stat files via read(like cat) or via fread (like nl) return different values. read() /proc/stat return right result, while fread() return wrong result. I had write simple C programs that just read or fread /proc/stat. read()…
shaun
  • 21
  • 5
1
vote
1 answer

if parent and child processes append to same file, do lseek() and write() need to be atomic?

Problem Statement One process opens a file for appending (assume there is no O_APPEND, appending here means first lseek() to the file end then write()), and then forks a child. The two dependent processes append to the file simultaneously. Assume…
David Chen
  • 1,777
  • 2
  • 12
  • 23
1
vote
1 answer

Why lseek with offset `-2` and not `-1` to read in reverse?

Why is it -2 and not -1? This is a part of a code that has to write a string from a file to another in reverse. Can you help me understand why it is -2 and not -1? while ( n >= 0) { read(fdin, &c, 1); write(fdout, &c, 1); …
1
vote
2 answers

Alternative to lseek to tail a file (Posix)

I had to implement a version of tail (posix system call). I did it using lseek and pread. (I reach the end of my file then I search for the right offset position, and then read from this position with pread, and writes to stdout till the end of the…
oajdwd
  • 13
  • 2
1
vote
2 answers

lseek() Trying to use with a byte file but pointer is of FILE type

This is for a project for everyone's awareness. It's my first project in C and have a question regarding lseek() and moving the file pointer. Right now I'm able to read the bitmap and DIB header of a bitmap file. I need to now traverse the…
Pwrcdr87
  • 935
  • 3
  • 16
  • 36
1
vote
1 answer

Why do we need lseek when pread accepts offset value?

I am having trouble to understand why lseek function is useful. Assuming I got a parameter like this given to me from the user: off_t offset = 10; And I wanted to read from the offset 100 bytes. I can use pread like this: void * buf =…
Eyzuky
  • 1,843
  • 2
  • 22
  • 45
1
vote
1 answer

lseek not working with append mode of file

int main(int argc, char *argv[]) { int i, j, count; int f1; char buf[16]; f1 = open(argv[1], O_RDWR | O_APPEND); if(f1 == -1) perror("open failed"); if(lseek(f1, 0, SEEK_SET) == -1) printf("lseek failed\n"); while(count = read(0, buf,…
sasha00
  • 23
  • 1
  • 1
  • 5