- When you want to read 2 blocks, use this array
float a[2][400];
.
- To read binary data, use binary mode in
fopen()
.
- Use
sizeof()
for the number of array elements and size of one element. So when you change the size of the array or use another type you must not change the code for reading.
- Because an array in C is allready a pointer, you don't need the &-operator for
a[i]
in
fread()
.
- When the data blocks are in serial order, you don't need a
fseek()
between the
fread()
calls. You even don't need a loop, you could read all data within one
fread()
call.
- The
long(3600+i*400*4)
is not a C typecast. It is a C++ copy constructor. This would compile in a C++ compiler but a C typecast is written
(long)3600+i*400*4
.
- Do some tests for error handling.
Finaly, it could look like this:
float a[2][400];
FILE *fp = fopen( "a.txt","rb");
if( NULL != fp)
{
size_t nElements = sizeof(a[0]) / sizeof(a[0][0]);
for( int i=0; i<2; i++)
{
long lOffset = 3600 + i * sizeof(a[0]);
if( 0 == fseek( fp, lOffset, SEEK_SET))
{
if( nElements != fread( a[i], sizeof(a[0][0]), nElements, fp))
{
break;
}
}
else
{
break;
}
}
}