22

Simple question,

When i use fread:

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

My file pointer, fp is moved ahead?

Manuel Selva
  • 18,554
  • 22
  • 89
  • 134
richardaum
  • 6,651
  • 12
  • 48
  • 64

4 Answers4

36

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.

DrBeco
  • 11,237
  • 9
  • 59
  • 76
Addicted
  • 1,694
  • 1
  • 16
  • 24
  • Ah, thanks for the reply. I was wondering what would have to use fseek to reposition the file pointer. – richardaum May 22 '12 at 06:37
  • 13
    This answer doesn't actually address the question that was asked which is simply what happens to the file pointer after calling fread – David Heffernan May 22 '12 at 06:53
  • Well, it wouldn't hurt to include an explanation of that in you answer also. – David Heffernan May 22 '12 at 06:57
  • 7
    Your answer should start with a "Yes" to answer the question from the title. – mrks Aug 20 '14 at 13:02
  • 1
    I've inverted the order, to reflect the thoughts expressed here in the comments. The answer was actually the small "edit" section in the end. I hope that change will improve the readers experience when searching for an answer. – DrBeco Dec 09 '17 at 16:26
31

Yes, calling fread does indeed move the file pointer. The file pointer will be advanced by the number of bytes actually read. In case of an error in fread, the file position after calling fread is unspecified.

David Heffernan
  • 601,492
  • 42
  • 1,072
  • 1,490
2

Yes, The fp will be advanced by the total amount of bytes read.
In your case the function fread reads sizeBlock objects, each sizeof(Reg) bytes long, from the stream pointed to by fp, storing them at the location given by ArrayA.

Jainendra
  • 24,713
  • 30
  • 122
  • 169
1

Yes, it does. It could be checked by using ftell() function in order to show current position (in fact, bytes read so far), take a look it:

int main() {

    typedef struct person {
        char *nome; int age;
    } person;

    // write struct to file 2x or more...

    FILE *file = fopen(filename, "rb");
    person p;
    size_t byteslength = sizeof(struct person);

    printf("ftell: %ld\n", ftell(file));
    fread(&p, byteslength, 1, file);
    printf("name: %s | age: %d\n", p.nome, p.idade);

    printf("ftell: %ld\n", ftell(file));
    fread(&p, byteslength, 1, file);
    printf("name: %s | age: %d\n", p.nome, p.idade);

    //...

    fclose(file);

    return 0;
}
felipsmartins
  • 13,269
  • 4
  • 48
  • 56