By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,169 Members | 1,065 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,169 IT Pros & Developers. It's quick & easy.

Data storage using fread( ) in vc++

100+
P: 105
Hi all,

I have read a data file which is actually a 3d matrix 100 X 100 X 80 into a buffer say "data" using fread function.When it reads how is data actually stored?Is it stored contigously and how to access them.
I am just using fread and no other manipulations in the function.Thanks in advance.
Mar 9 '07 #1
Share this Question
Share on Google+
4 Replies


Expert 100+
P: 1,510
Hi all,

I have read a data file which is actually a 3d matrix 100 X 100 X 80 into a buffer say "data" using fread function.When it reads how is data actually stored?Is it stored contigously and how to access them.
I am just using fread and no other manipulations in the function.Thanks in advance.
assuming you have an array
Expand|Select|Wrap|Line Numbers
  1. int data[100][100][80];
  2.  
C stores multi-dimensional arrays in memory such that the rightmost subscript(s) varies fastest, i.e. a two-dimensional two row by three column matrix would be stored in memory as a sequence of three rows each two columns wide:
Expand|Select|Wrap|Line Numbers
  1.   +-----------------------------------------------------------------------------+
  2.   ¦matrix[0][0]¦matrix[0][1]¦matrix[0][2]¦matrix[1][0]¦matrix[1][1]¦matrix[1][2]¦
  3.   +-----------------------------------------------------------------------------+
  4.  
if it was written so
Expand|Select|Wrap|Line Numbers
  1.   result = fwrite(data,sizeof(int),100*100*80,pFile);
  2.  
you would read it so
Expand|Select|Wrap|Line Numbers
  1.   result = fread (data,sizeof(int),100*100*80,pFile);
  2.   if (result != 100*100*80) {fputs ("Reading error",stderr); exit (3);}
  3.  
if it was written by a fortran program which store arrays in column major order you would have to read int by int into the correct element
Mar 9 '07 #2

100+
P: 105
Hi Horace1,

Thank you for your reply.So, now if I want to access all rows and columns of 2nd in 3rd dimension, eg: data[10][10][3].I want to have access all the 10 x10 elements of the 2nd in the last dimension.Could you just tell me how can I do that.Hope you understood my question.Thanks.
Mar 9 '07 #3

Expert 100+
P: 1,510
Hi Horace1,

Thank you for your reply.So, now if I want to access all rows and columns of 2nd in 3rd dimension, eg: data[10][10][3].I want to have access all the 10 x10 elements of the 2nd in the last dimension.Could you just tell me how can I do that.Hope you understood my question.Thanks.
don't understand what you need to do. You index the array elements as you would any 1 oe 2D array, e.g. to print the elements
Expand|Select|Wrap|Line Numbers
  1.   for(i=0;i<100;i++)
  2.    for(j=0;j<100;j++)
  3.     {
  4.     for(k=0;k<80;k++) 
  5.      printf("%d ", data[i][j][k]);
  6.      printf("\n");
  7.     }
  8.  
Mar 9 '07 #4

100+
P: 105
Hi Horace1,

Sorry for not being claer abt my problem.Here it is.

Actually I have a data file image.v and I have to read it.Its matrix dimensions are specified in the header file as 100x100x80.Now I have to read this image and access the elements.

Already I have read ( ) memeber function that can read data file using fread ( ) operator.
Snippet of the code:

void A <T>::read(T * const data, const uint64 _size) (A is a class)

{ uint64 bytes;
char *dp;

bytes=_size*sizeof(T);
dp=(char *)data;
do { uint64 read_bytes, s;

s=bytes;

read_bytes=fread(dp, 1, (size_t)s, file);
if (read_bytes > 0) { bytes-=read_bytes;
dp+=read_bytes;
}

Now I am using this read function in my project as

A<float> *theRawptr=NULL;
float *imgdata;
imgdata=new float[imgsize];
theRawptr=new A <float>(theObj.data_file(),false,false);
theRawptr->read(imgdata,imgsize);

Here I have calculated the image size and theObj.datafile() is the name of the data file I am trying to access.Now when I say imgdata[200] it gives me some value.
So my question is is this imgdata a single dimensional array???I am thinking the way I have read it it is 1D.
If that is the case is there any way I can read it as matrix???

Sorry if there is lot of confusion.Thanks a lot for all your help.
Mar 9 '07 #5

Post your reply

Sign in to post your reply or Sign up for a free account.