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
11
votes
4 answers

Write to file without zero fill in Linux

Let's say that the intent is to create a file with a large hole at the beginning that we will write to later on, on an embedded device running Linux. We open the file, obtain a file descriptor and call lseek on it to seek to a certain, known…
11
votes
3 answers

Is lseek() O(1) complexity?

I know that my question has an answer here: QFile seek performance. But I am not completely satisfied with the answer. Even after looking at the following implementation of generic_file_llseek() for ext4, I can't seem to understand how can the…
Nehal J Wani
  • 16,071
  • 3
  • 64
  • 89
7
votes
2 answers

Writing and reading to linux /proc/... filesystem without lseek()

In this source code http://man7.org/tlpi/code/online/dist/sysinfo/procfs_pidmax.c.html the file /proc/sys/kernel/pid_max is first simply read (using the read syscall) and then simply written (using the write syscall). Why is it no necessary to lseek…
levzettelin
  • 2,600
  • 19
  • 32
5
votes
1 answer

How does `lseek` help determine whether a file is empty?

I am looking at the source code of cat from the GNU coreutils, in particular the circle detection. They are comparing device and inode and that works fine, there is however an extra case where they allow the output to be an input, if the input is…
Niklas Mohrin
  • 1,756
  • 7
  • 23
4
votes
2 answers

Why using SEEK_END doesn't work here?

So I'm trying to learn C file IO operations from a snippet from the university. My problem is that SEEK_END doesn't work as I expect it to work. Let met give you more details: input.txt: abcd-abcd-abcd code: int fd, fdr, l1, l2, wb1, wb2; char…
3
votes
1 answer

seek() and read() from a file opened with O_DIRECT

I am trying to seek and read from a file and my objective is that all reads come directly from disk. In order to do this, I open() the file with O_DIRECT, lseek() to the required offset, and try to read() a block from disk. I encounter an error…
Shehbaz Jaffer
  • 1,944
  • 8
  • 23
  • 30
3
votes
1 answer

what does `l` in `lseek` of unistd.h mean?

I am reading APUE to explore the details of C and Unix, and encounter lseek NAME lseek - move the read/write file offset SYNOPSIS #include off_t lseek(int fildes, off_t offset, int whence); What does l mean, is it length?
AbstProcDo
  • 19,953
  • 19
  • 81
  • 138
3
votes
1 answer

File locking + Fscanf/Lseek

I have a file called "input.txt" that holds some values. I'm writing program that will find minimum value in that file, and replace that minimum with number given as command line argument - if that command line argument is bigger then minimum. These…
Aleksandar Makragić
  • 1,957
  • 17
  • 32
3
votes
1 answer

SEEK_HOLE always point to the end of file

I'm dealing with the problem of APUE to write a program somehow like cp to copy files(Chapter 4 Problem 4.6). If the file contains holes(or sparse files) '\0's in the gaps shall never be coped. The ideal approach is to read and write block by block,…
user1299979
2
votes
2 answers

reset the file location using lseek() system call

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…
Fred
  • 111
  • 1
  • 6
2
votes
1 answer

Implementing lseek in xv6

First off I need to say it's completely possible I'm missing something. My assignment is to essentially implement 'fprintf'. Now while appending to the file isn't required, I like to go above and beyond. My issue is, I can't find a definition for…
Flacarile
  • 69
  • 5
2
votes
2 answers

How to properly use lseek() to extend file size?

I'm trying to truly understand the use of lseek() while creating a file of the needed size. So I wrote this code whose only goal is to create a file of the size given in the input. Running for example: $ ./lseek_test myFile 5 I would expect it to…
Robb1
  • 4,587
  • 6
  • 31
  • 60
2
votes
1 answer

Custom SPI driver to implement lseek

I am trying to implement a SPI driver for custom hardware. I have started with a copy of the spidev driver, which has support for almost everything I need. We're using a protocol that has three parts: a command bit (read / write) an address, and an…
2
votes
1 answer

Print last 10 lines of file or stdin with read write and lseek

I'm working on an implementation of the tail function and I'm only supposed to use read(), write() and lseek() for I/O, and so far I have this: int printFileLines(int fileDesc) { char c; int lineCount = 0, charCount = 0; int pos = 0,…
2
votes
1 answer

How to find out if offset cursor is at EOF with lseek()?

How can I find out if the offset cursor is currently at EOF by using lseek() only?
JohnnyFromBF
  • 9,873
  • 10
  • 45
  • 59
1
2 3 4 5 6