Read in File to 2d Array C++

Hi,
I am trying to read the contents of a text file into a 2D array in a macro and this attempt crashes my plan. Where am I going wrong?

                        1
2
3
4
5
6
seven
8
ix
10
11
12
thirteen
14
15
16
17
18
xix
20
                                                  int                          Nts = 4;                          // number of rows                          int                          Nnd = 14;                          // number of colums                          float                          **crd;  DEFINE_ON_DEMAND(Move_By_Filedod) {                          int                          i = 0;                          bladder                          dats; 	FILE* fp;  	fp = fopen("GGG.dat","r");                          // file has 4 rows and fourteen columns                          crd = malloc(Nts*sizeof(bladder                          *));                          for                          (i=0;i<Nts;i++) 	crd[i] = malloc(Nnd*sizeof(float));  	fread(crd,                          sizeof                          crd,Nts*Nnd,fp); 	fclose(fp);         printf("Exam: %f \n",crd[0][0]); }                      

Final edited on

Could you not do this with C++? Something similar:

                        1
2
3
4
v
6
vii
8
9
10
11
                        std::vector<std::vector<float>> the2DVector (Nts, std::vector<float>(Nnd));  ifstream inFile("GGG.dat");                          for                          (int                          i = 0 ; i < Nts; ++i) {                          for                          (int                          j = 0 ; j < Nnd; ++j)   {     inFile >> the2DVector[i][j];   } }                      

Anyway, this looks suspicious.
fread(crd, sizeof crd,Nts*Nnd,fp);
Are these values correct? sizeof crd is the size of a arrow, but you want to read bladder values. Are they the same size? And in what format is the file? Is it binary or text? You lot're reading raw bytes, and then if the file is text, this will go very wrong.

Last edited on

You malloc the rows of the assortment separately, in different resource allotment blocks, and then they are not contiguous. Simply yous try to read it in as if all of the rows were contiguous. You either need to allocate the data block in one malloc phone call or practise a separate fread for each row. I would do the one-time.

Too, you should open the file in binary mode since you are reading binary information. If the data file isn't binary (i.e., you can easily read it in a text editor) then yous shouldn't exist using fread.

And your fread telephone call is not quite right. Information technology should be sizeof **crd or sizeof crd[0][0], since each element is the size of a float, not the size of a pointer to a pointer to a float. And y'all don't desire to read it into the aforementioned memory where you've stored your row pointers.

So perhaps something like this:

                        one
2
3
4
5
6
7
8
9
x
eleven
                        DEFINE_ON_DEMAND(Move_By_Filedod) {     crd = malloc(Nts *                          sizeof                          *crd);                          // classify infinite for the row pointers                          crd[0] = malloc(Nnd * Nts *                          sizeof                          **crd);                          // allocate space for the information cake                          for                          (int                          i = one; i < Nts; i++)         crd[i] = crd[i - 1] + Nnd;                          // calculate the row pointers                          FILE *fp = fopen("GGG.dat","rb");                          // open up in binary manner                          fread(&crd[0][0],                          sizeof                          crd[0][0], Nts * Nnd, fp);                          // read into the data block                          fclose(fp); }                      

Concluding edited on

Howdy,

I am using a text file, non binary. I got this example from a c++ book but it simply read into a 1D assortment, then that is where I got the idea to use fread for a 2nd array as well. When I used the snippet suggested by dutch I did get another crash. Is it because I am using the text information? I want to attain this as low level commands and as quickly executed equally possible considering this file is only a test file. The existent file volition take 1500 rows and 6000 columns. My information file looks like this, with tabs between columns:

91.00000000000000 97.00000000000000 94.00000000000000
0.007000000000000 0.010000000000000 0.012000000000000
0.003333333333333 0.000300000000000 0.008333337633333

Besides, to Repeater I cannot employ the Vector class. I must use regular arrays.

Thank you for everyone's input so far!

P.S. I got the array initialization with malloc from this folio:

https://world wide web.eskimo.com/~scs/cclass/int/sx9b.html

Final edited on

I think there might be something else incorrect with the code provided by dutch, considering fifty-fifty this errors, without any file reading or annihilation.

                        1
2
3
4
five
6
7
8
9
ten
                                                  crd = malloc(Nts *                          sizeof                          *crd);// allocate space for the row pointers                          crd[0] = malloc(Nnd * Nts *                          sizeof                          **crd);                          // allocate space for data blocks                          for                          (int                          i=0;i<Nts;i++) 			crd[i] = crd[i-1] + Nnd;                          for                          (int                          i=0;i<Nts;i++) 	{                          for                          (int                          j=0;j<Nnd;j++) 			crd[i][j] = 12+3*i+j; 	}                      

Last edited on

Yes, information technology crashed because your data is not binary.

And the lawmaking y'all posted doesn't work because you f'ed it upwards!
You changed the starting value of i to 0 where I had it equally i (we set up element 0 in the previous line).

1500 rows past 6000 columns is not that big.
Don't worry nearly "efficiency".
Just practice it in the simplest way you can.
And forget about fread. Information technology is only for binary data.
Utilise fscanf with "%f" format instead.

Last edited on

Ah, I see I re-used my previous for loop because I didn't notice that yours started from i. Thanks dutch! At present it doesn't crash when I make that alter, only it says all elements of the array are zero. I estimate because it is not binary.

My understanding is that fscanf will read a whole row at a fourth dimension and I don't know how I can peradventure give 6000 %f symbols in the telephone call, which is why I thought fread would piece of work. How would you recommend doing this? Is there something like repmat in matlab?

Thank you again!

No, you don't need to read a line at a time. Endeavour something like this:

                        1
2
3
                                                  for                          (int                          r = 0; r < Nts; ++r)                          for                          (int                          c = 0; c < Nnd; ++c)             fscanf(fp,                          "%f", &crd[r][c]);                      

Really, looking at the input values that you've displayed above, yous may want to apply double instead of float to hold them. Floats only accept about seven digits of precision whereas doubles have nearly 16. The format spec for doubles is "%lf" (that'south a lowercase 50 before the f).

Last edited on

Thanks, dutch!

Topic archived. No new replies allowed.

Read in File to 2d Array C++

Source: http://www.cpp.re/forum/general/266592/

0 Response to "Read in File to 2d Array C++"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel