fseek

How do you determine the size of a file (in C) for files that are larger than 4GB?

The code currently does this and the fgetpos does handle files larger than 4GB but the seek returns an error, so any idea how to seek to the end of a file >4GB? fpos_t currentpos; sok=fseek(fp,0,SEEK_END); assert(sok==0,"Seek error!"); fgetpos(fp,&currentpos); m_filesize=currentpos; ...

fseek / rewind in a loop

I have a situation in a code where there is a huge function that parses records line-by-line, validates and writes to another file. In case there are errors in the file, it calls another function that rejects the record and writes the reject reason. Due to a memory leak in the program, it crashes with SIGSEGV. One solution to kind of ...

Using fseek to backtrack

Is using fseek to backtrack character fscanf operations reliable? Like for example if I have just fscanf-ed 10 characters but I would like to backtrack the 10 chars can I just fseek(infile, -10, SEEK_CUR) ? For most situations it works but I seem to have problems with the character ^M. Apparently fseek registers it as a char but fscan...

PHP fseek() equivalent for variables?

Hi, What I need is an equivalent for PHP's fseek() function. The function works on files, but I have a variable that contains binary data and I want to work on it. I know I could use substr(), but that would be lame - it's used for strings, not for binary data. Also, creating a file and then using fseek() is not what I am looking for ei...

C malloc/free + fgets performance

As I loop through lines in file A, I am parsing the line and putting each string (char*) into a char**. At the end of a line, I then run a procedure that consists of opening file B, using fgets, fseek and fgetc to grab characters from that file. I then close file B. I repeat reopening and reclosing file B for each line. What I would ...

Question about file seeking position..

My previous Question is about raw data reading and writing, but a new problem arised, it seems there is no ending.... The question is: the parameters of the functions like lseek() or fseek() are all 4 bytes. If i want to move a span over 4G, that is imposible. I know in Win32, there is a function SetPointer(...,Hign, Low,....), this po...

why fseek or fflush is always required between reading and writing in the read/write "+" 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 string you're trying to overwrite, and because an fseek or ff...

C-programming ftell fseek fread, read size of a file

I have a file. I read the size of file. Then I loop reading two bytes at a time until I get to the end of the file. After every read operation I increment the current position by 2, however the position does not get incremented after I get to half the size of the file, the fread operation will read 0 bytes. the program reads the file si...

What's the intended use of _fread_nolock, _fseek_nolock?

Hi there, we have a C++ class which basically reads and writes vectors from a binary file. An exemplary read function that loads a single vector into memory looks like this: int load (const __int64 index, T* values) const { int re = _fseeki64(_file, index * _vectorSize + _offsetData, SEEK_SET); assert(re == 0); size_t read = frea...

Reading the last 50 characters of a file with fseek()

I'm trying to read the last 50 characters in a file by doing this: FILE* fptIn; char sLine[51]; if ((fptIn = fopen("input.txt", "rb")) == NULL) { printf("Coudln't access input.txt.\n"); exit(0); } if (fseek(fptIn, 50, SEEK_END) != 0) { perror("Failed"); fclose(fptIn); exit(0); } fgets(sLine, 50, fptIn); printf("%s", ...

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 in the middle of the file. Supposing I have a file with 1MB data and then I insert somethi...

Read file in array line by line

Can you set any index of array as starting index i.e where to read from file? I was afraid if the buffer might get corrupted in the process. #include <stdio.h> int main() { FILE *f = fopen("C:\\dummy.txt", "rt"); char lines[30]; //large enough array depending on file size fpos_t index = 0; while(fgets(&lines[index], ...

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 ...

optimal way to prepend a file in php

how do I do this. the file can get very big, so good performance is necessary code is helpful. this is what i have so far function prepend($string, $filename) { $context = stream_context_create (); $fp = fopen($filename,'r',1,$context); $tmpname = md5($string); file_put_contents($tmpname,$string); file_put_co...

Appending a data to file in c

I require to add a string before 45byte in an existing file. I tried using fseek as bellow. int main() { FILE *fp; char str[] = "test"; fp = fopen(FILEPATH,"a"); fseek(fp,-45, SEEK_END); fprintf(fp,"%s",str); fclose(fp); return(0); } I expected that this code will add "test" in before 45 char from EOF...

Reading a specific number of lines from a file in C (scanf, fseek,fgets)

I have a process master that spawns N child processes that communicate with the parent through unnamed pipes. I must be able to: make the father open the file and then send, to each child, a struct telling that it has to read from min to max line; this is going to happen at the same time, so I don't know: 1st how to divide total_lines ...

Array of Linked lists on disk

I am trying to find how to store and process (search, add, remove) an array of linked lists on disk. For example in memory, it would like struct list { int a; struct list *next; }LIST LIST *array[ARRAY_SIZE] int main{ ... LIST *foo = array[pointer]; /* Search */ while(foo!=NULL){ ... foo=foo->next } } I believe that by ...

C Program: How to properly use lseek() or fseek() to modify a certain part of a file?

I have a binary file in CSV format that contains multiple records. Each record is entered in as user_name, last_name, first_name, num_wins, num_losses, num_ties. I am creating a method to update a certain players record of wins,losses and ties. The user inputs the username to change followed by the changes to the wins, losses and ties. ...