Simple question,
When i use fread:
fread(ArrayA, sizeof(Reg), sizeBlock, fp);
My file pointer, fp
is moved ahead?
fread will read from where it left off the last time round the loop. You should check the return value from fread .
The fwrite subroutine does not change the contents of the array pointed to by the Pointer parameter.
Yes, the file pointer is automatically moved by read and write operations. ...and not seeking improves the performance a lot.
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 .
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:
limit
elements of size size
, or 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With