Questions tagged [seek]

Seeking is the act of moving around a file or file-like object.

Seeking is the act of moving around a file or file-like object.

ANSI C in section 2.3.12.10 specifies thatfseek has the argument structure:

int fseek(FILE *stream, long offset, int whence);

Where:

  • stream is the FILE object to seek (note that this does not have to physically be a file)
  • offset is how much, relative to the whence argument
  • whence is one of
    • SEEK_SET (offset is computed relative to the start of the file)
    • SEEK_CUR (offset is computed relative to the current position)
    • SEEK_END (offset is computed relative to the end of the file)

The majority of languages based upon C, and even many that aren't, have inherited something similar to fseek, but have probably put it somewhere else (ex. Python's file.seek). Check your language of choice's documentation for details.

537 questions
16
votes
2 answers

Does FileInputStream.skip() do a seek?

I want to copy the last 10MB of a possibly large file into another file. Ideally I would use FileInputStream, skip() and then read(). However I'm unsure if the performance of skip() will be bad. Is skip() typically implemented using a file seek…
Mike Q
  • 22,839
  • 20
  • 87
  • 129
16
votes
3 answers

Read specific bytes of file in python

I want to specify an offset and then read the bytes of a file like offset = 5 read(5) and then read the next 6-10 etc. I read about seek but I cannot understand how it works and the examples arent descriptive enough. seek(offset,1) returns what?…
user3124171
  • 401
  • 2
  • 7
  • 19
14
votes
4 answers

iOS Audio Trimming

I searched a lot and couldn't find anything relevant... I am working on iOS audio files and here is what I want to do... Record Audio and Save Clip (Checked, I did this using AVAudioRecorder) Change the pitch (Checked, Did this using…
Muhammad Usama
  • 247
  • 1
  • 2
  • 8
14
votes
5 answers

Write to the middle of an existing binary file c++

I'm trying to open a binary file for writing without erasing the content. But I do not want to write to eof. I want to write to a specific position in file. Here is a litte example: ofstream out("test.txt", ios::binary | ios::app); for(int i = 0; i…
Jonny Schubert
  • 1,393
  • 2
  • 18
  • 39
14
votes
2 answers

File reading performance on smartphones: internal storage vs. SD card vs. PC hard disk

My Android application will use big and very big files (i.e. between the size of 10MB and 2GB). I've always been wondering about what hardware is used by smartphones for stable storage, and whether the software (file reading/seeking) considerations…
Thomas Calc
  • 2,994
  • 3
  • 30
  • 56
13
votes
3 answers

Android exoplayer callback when seek is done

I am using exoplayer on Android. I need to show progress indicator when the video is seeking . I can start showing progress indicator on seekTo method but which callback method should I use to hide progress indicator when the seek is done.
Prabhu M
  • 3,534
  • 8
  • 48
  • 87
13
votes
7 answers

c++ fastest way to read only last line of text file?

I would like to read only the last line of a text file (I'm on UNIX, can use Boost). All the methods I know require scanning through the entire file to get the last line which is not efficient at all. Is there an efficient way to get only the last…
user788171
  • 16,753
  • 40
  • 98
  • 125
11
votes
3 answers

Python: rewinding one line in file when iterating with f.next()

Python's f.tell doesn't work as I expected when you iterate over a file with f.next(): >>> f=open(".bash_profile", "r") >>> f.tell() 0 >>> f.next() "alias rm='rm -i'\n" >>> f.tell() 397 >>> f.next() "alias cp='cp -i'\n" >>> f.tell() 397 >>>…
new name
  • 15,861
  • 19
  • 68
  • 114
11
votes
4 answers

Python seek on remote file using HTTP

How do I seek to a particular position on a remote (HTTP) file so I can download only that part? Lets say the bytes on a remote file were: 1234567890 I wanna seek to 4 and download 3 bytes from there so I would have: 456 and also, how do I check if…
Marconi
  • 3,601
  • 4
  • 46
  • 72
10
votes
2 answers

How in portable C to seek forward when reading from a pipe

Since fseek() does not work on pipes what methods exist for simulating seeking forward? The naive approach is to use fread() and throw away the contents read into the memory buffer. For huge seeks to avoid huge buffers you would use the same buffer…
hippietrail
  • 15,848
  • 18
  • 99
  • 158
10
votes
1 answer

Is using istream::seekg too much expensive?

In c++, how expensive is it to use the istream::seekg operation? EDIT: How much can I get away with seeking around a file and reading bytes? What about frequency versus magnitude of offset? I have a large file (4GB) that I am parsing, and I want to…
Brian
  • 3,453
  • 2
  • 27
  • 39
10
votes
4 answers

f.seek() and f.tell() to read each line of text file

I want to open a file and read each line using f.seek() and f.tell(): test.txt: abc def ghi jkl My code is: f = open('test.txt', 'r') last_pos = f.tell() # get to know the current position in the file last_pos = last_pos + 1 f.seek(last_pos) # to…
John
  • 3,888
  • 11
  • 46
  • 84
9
votes
2 answers

Is it safe to use lseek() when reading from Proc-FS files second time

Is it safe to use lseek(fd,0) and then read(fd,buf) for /proc/stat file instead of reopening it to get updated contents of this file next time? And what does the mmap() call after opening this file really do (see below)? The problem I am…
user389238
  • 1,656
  • 3
  • 19
  • 40
9
votes
1 answer

Obtaining the last character in a stringstream without copying its whole buffer

If I use this code: template /* ... */ std::stringstream ss; ss << function_yielding_a_Streamable(); auto last_char = ss.str().back(); then (I believe) a copy of the string in ss's buffer will need to be created, just for me…
einpoklum
  • 118,144
  • 57
  • 340
  • 684
9
votes
2 answers

How to write at a particular position in text file without erasing original contents?

I've written a code in Python that goes through the file, extracts all numbers, adds them up. I have to now write the 'total' (an integer) at a particular spot in the file that says something something something...Total: __00__ something…
learnerX
  • 1,022
  • 1
  • 18
  • 44
1
2
3
35 36