Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does fread move the file pointer?

Tags:

Simple question,

When i use fread:

fread(ArrayA, sizeof(Reg), sizeBlock, fp); 

My file pointer, fp is moved ahead?

like image 685
richardaum Avatar asked May 22 '12 06:05

richardaum


People also ask

Does fread pick up where it left off?

fread will read from where it left off the last time round the loop. You should check the return value from fread .

Does Fwrite move the file pointer?

The fwrite subroutine does not change the contents of the array pointed to by the Pointer parameter.

Does read move the file pointer?

Yes, the file pointer is automatically moved by read and write operations. ...and not seeking improves the performance a lot.

Does Fclose delete the pointer?

1) fclose can't set the FILE* to NULL, because it's parameter is a FILE*, so it recieves the FILE* by value. Since it's a copy, it cannot alter your pointer, unless the parameter was changed to a FILE*&. 2) In C++, you pretty much never call delete unless you called new .


1 Answers

Answer: Yes, the position of the file pointer is updated automatically after the read operation, so that successive fread() functions read successive file records.

Clarification: fread() is a block oriented function. The standard prototype is:

size_t fread(void *ptr,              size_t size,              size_t limit,              FILE *stream); 

The function reads from the stream pointed to by stream and places the bytes read into the array pointed to by ptr, It will stop reading when any of the following conditions are true:

  • It has read limit elements of size size, or
  • It reaches the end of file, or
  • A read error occurs.

fread() gives you as much control as fgetc(), and has the advantage of being able to read more than one character in a single I/O operation. In fact, memory permitting, you can read the entire file into an array and do all of your processing in memory. This has significant performance advantages.

fread() is often used to read fixed-length data records directly into structs, but you can use it to read any file. It's my personal choice for reading most disk files.

like image 192
Addicted Avatar answered Oct 01 '22 20:10

Addicted