Questions tagged [fseek]

fseek is a C function belonging to the ANSI C standard library, and included in stdio.h. Its purpose is to change the file position indicator for the specified file or stream.

fseek() is a C function belonging to the ANSI C standard library, and included in the file stdio.h. Its purpose is to change the file position indicator for the specified file or stream. Because fseek uses 32 bit values on many platforms it has a limitation of maximum 2 gigabyte seeks. fseeko64 would be used for larger offsets.

See also

419 questions
68
votes
2 answers

Append to the end of a file in C

I'm trying to append the contents of a file myfile.txt to the end of a second file myfile2.txt in c. I can copy the contents, but I can't find a way to append. Here's my code: FILE *pFile; FILE *pFile2; char buffer[256]; pFile=fopen("myfile.txt",…
Sonofblip
  • 1,167
  • 3
  • 12
  • 20
49
votes
2 answers

What is the use of zero offset in fseek() function with SEEK_CUR?

while (fread(&product, sizeof(Product), 1, file) == 1) { product.price *= 2.0; fseek(file, -sizeof(Product), SEEK_CUR); fwrite(&product, sizeof(Product), 1, file); fseek(file, 0, SEEK_CUR); } Using the code above, I…
osmangokalp
  • 611
  • 4
  • 7
25
votes
1 answer

fseek does not work when file is opened in "a" (append) mode

FILE* f = fopen("rajat", "w"); fputs("sometext", f); fseek(f, 6, SEEK_SET); fputs("is a", f); fclose(f); Successfully returns: "someteis a" But FILE* f = fopen("rajat", "a"); fputs("sometext", f); fseek(f, 6, SEEK_SET); fputs("is a",…
Rajat
  • 1,766
  • 2
  • 21
  • 42
24
votes
6 answers

How is fseek() implemented in the filesystem?

This is not a pure programming question, however it impacts the performance of programs using fseek(), hence it is important to know how it works. A little disclaimer so that it doesn't get closed. I am wondering how efficient it is to insert data…
pajton
  • 15,828
  • 8
  • 54
  • 65
24
votes
7 answers

Fastest way to read every 30th byte of large binary file?

What is the fastest way to read every 30th byte of a large binary file (2-3 GB)? I've read there are performance problems with fseek because of I/O buffers, but I don't want to read 2-3 GB of data into memory before grabbing every 30th byte either.
K_T
  • 591
  • 7
  • 22
19
votes
2 answers

fseek vs rewind?

I have noticed two methods to return to the beginning of a file FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); rewind(fp); and FILE *fp = fopen("test.bin", "r") fseek(fp, 0, SEEK_END); fseek(fp, 0, SEEK_SET); What would be difference…
Zombo
  • 1
  • 62
  • 391
  • 407
18
votes
4 answers

the output of ftell in php function

Here is my code:
showkey
  • 482
  • 42
  • 140
  • 295
17
votes
4 answers

fseek on a position beyond EOF does not trigger EOF using feof, how come?

I'm reading data from a file to memory that is opened with: FILE *f = fopen(path, "rb"); Before I start copying bytes from the file I seek to a start position using: /** …
rzetterberg
  • 10,146
  • 4
  • 44
  • 54
16
votes
1 answer

Understanding undefined behavior for a binary stream using fseek(file, 0, SEEK_END) with a file

The C spec has an interesting footnote (#268 C11dr §7.21.3 9) "Setting the file position indicator to end-of-file, as with fseek(file, 0, SEEK_END), has undefined behavior for a binary stream (because of possible trailing null characters) or for…
chux - Reinstate Monica
  • 143,097
  • 13
  • 135
  • 256
16
votes
3 answers

Why is fseek or fflush always required between reading and writing in the update modes?

Q: I'm trying to update a file in place, by using fopen mode "r+", reading a certain string, and writing back a modified string, but it's not working. A: Be sure to call fseek before you write, both to seek back to the beginning of the…
pierrotlefou
  • 39,805
  • 37
  • 135
  • 175
15
votes
2 answers

Complexity of f.seek() in Python

Does f.seek(500000,0) go through all the first 499999 characters of the file before getting to the 500000th? In other words, is f.seek(n,0) of order O(n) or O(1)?
Irrelevant
  • 158
  • 1
  • 6
15
votes
10 answers

Read a file backwards line by line using fseek

How do I read a file backwards line by line using fseek? code can be helpful. must be cross platform and pure php. many thanks in advance regards Jera
Jeremy Gwa
  • 2,333
  • 7
  • 25
  • 31
14
votes
5 answers

Using fseek and ftell to determine the size of a file has a vulnerability?

I've read posts that show how to use fseek and ftell to determine the size of a file. FILE *fp; long file_size; char *buffer; fp = fopen("foo.bin", "r"); if (NULL == fp) { /* Handle Error */ } if (fseek(fp, 0 , SEEK_END) != 0) { /* Handle…
Frank
  • 18,432
  • 9
  • 30
  • 30
14
votes
1 answer

ftello/fseeko vs fgetpos/fsetpos

What is the difference between ftello/fseeko and fgetpos/fsetpos? Both seem to be file pointer getting/setting functions that use opaque offset types to sometimes allow 64 bit offsets. Are they supported on different platforms or by different…
Jeremy Salwen
  • 8,061
  • 5
  • 50
  • 73
13
votes
3 answers

Does fseek() move the file pointer to the beginning of the file if it was opened in "a+b" mode?

I wish to open a file using the "a+b" mode, i.e. if it does not exist it is created automatically, but if it does I don't want to overwrite it. I want to be able to read and write to the file. The file is binary, and I want to save records of a…
jbx
  • 21,365
  • 18
  • 90
  • 144
1
2 3
27 28