Here is my scenario i have a file 2000 bytes long. I want to read bytes 500 - 1500 so that i have the middle 1000 bytes stored. I then want to disect the 1000 bytes into 20 bte chunks and preform calculations on ever 20 byte sectiong until the end of the 1000 bytes. I have the following code and i just want to check that what i have is a sensible way of doing things and if it wil actually work!

char *buffer1 = new char[ROOT_DIR_SIZE][Size];  //ROOT_DIR_SIZE = to 1000, Size =      to 20, these have been intialised earlier

f.seekg (START_OF_ROOT,std::ios::beg) ; // positions he file at the 500th byte, 'f' is the file stream


for(i=0;i<ROOT_DIR_SIZE;++i)
{ 


f.read(buffer1, ROOT_DIR_SIZE*sizeof(char)); // read until the 1000 byte buffer is full


         for(j=0;j<Size;++j)
        {

         f.read(buffer1, Size*sizeof(char));  //read until the 20 byte buffer is full
          
         //any calculations on the 20 bytes would have to occur in here???
         }

}

My question is will this keep reading the 20 byte chunks until the end of the 1000 byte buffer is reached?


thanks

If you want to put aside a 2-dimensional array, you could do this:

char buffer1[1000][20];

This puts aside 20,000 bytes. But do you want 20,000 bytes? You said you are only storing bytes 500 - 1500, which is 1001 bytes. If you want to do a 2-D array with 20 byte chunks, you could do:

char buffer1[50][20];

That's 1000 bytes, so you'd be one byte short if you need bytes 500 - 1500. Or you could do a one dimensional array of 1001 bytes. Do you need to store more than 1001 bytes for this?

i guess that just a single array mite be best for this job. On checking again i would like to read 32 bytes each time. I know i wil have eft over bytes but im not wooried about this at present. Anyway if is intialise the first array

char *buffer1 = new char[ROOT_DIR_SIZE];

this should take the 1000 bytes. But im now stuck. If i declare another array of size 32 how would i read 32 bytes into the second array from the first array by using the second loop?

OK, so declare char buff[20][50]; // 1000 bytes of contiguous memory, which you can loop over in 50 byte blocks.

Then do f.read( buff, sizeof buff ); to read all 1000 bytes (assuming you've seeked to the correct place)

Then do the for ( i = 0 ; i < 20 ; i++ ) for ( j = 0 ; j < 50 ; j++ ) bit to process the 2D array

Thank you for you help.

In the case below that you stated

char buff[20][50];

I have 20 stored in a variable called 'Size' and 50 stored in a variable 'ROOT_DIR_SIZE' i thought i would b able to declare it as below but it doesnt seem t work. I have previous declared single arrays this was and im presuming that the prblem comes with adding to the end?

can anyone help me with the declaration?

char *buffer1 = new char[ROOT_DIR_SIZE][Size];

I also have a question about the 2d array as ive never really used these before. I have basically used your code below and am i right in saying that if i place code where i have indicated below it will prform the same calculations on every chunk of 20 bytes?

for ( i = 0 ; i < 20 ; i++ ) 

{

for ( j = 0 ; j < 50 ; j++ )
{
//code in here that will preform calculations on every 20 byte chunk
}


}

THANKS LOADS

To the first question, if you want 50 blocks of twenty characters each, you can do this:

char** buffer1;
buffer1 = new char*[50];
for (int i = 0; i < 50; i++)
   buffer1[i] = new char[20];

If you want twenty blocks of fifty characters each, reverse the 20's and 50's above. I think you want 50 blocks of twenty characters each if I read your original problem correctly. The loop below does 20 calculations on 50 bytes each:

for ( i = 0 ; i < 20 ; i++ ) 

{

for ( j = 0 ; j < 50 ; j++ )
{
//code in here that will perform calculations on every 50 byte chunk
}


}

If you want 50 calculations on groups of twenty bytes each, reverse the 20 and 50 above like this:

for ( i = 0 ; i < 50 ; i++ ) 

{

for ( j = 0 ; j < 20 ; j++ )
{
//code in here that will perform calculations on every 20 byte chunk
}


}

If you are doing the same thing on each twenty byte block, I think you have a reasonable skeleton using the above nested loop.

Not sure if the above 2-dimensional declaration is the only or most efficient way to declare the 2-D array dynamically, which seems to be the way you want to go. Does anybody else know of a better way?

i have the following code. Im pretty sure this should read the 1000 bytes then split it into 50 sections of 20 bytes

int i,j;
char buff[20][50];

for ( i = 0; i < 50; i++ )

f.read(buff, 50*sizeof(char));


{

for ( j = 0; j < 20; j++ )
{

f.read(buff, 20*sizeof(char));

}


}

Well it doesn't.

This might

for ( i = 0; i < 50; i++ ) {
    f.read(buff[i], sizeof(buff[i]));
    for ( j = 0 ; j < 20 ; j++ ) {
        // do stuff with buff[i][j]
    }
}

I have implimented code as follows. This should read 512 blocks of 32 bytes. On every 32 bytes it should store each byte as a single variable so that i can preform calculations. I printed a test value at the end but for some reason it prints it out LOADS of times but i cant understand why. can anyone see what im doing wrong? Or if im doing anything right!

int i,j;
char buff[512][32];
f.seekg (START_OF_ROOT,std::ios::beg);



for ( i = 0; i < 512; i++ )

{
    f.read(buff[i], sizeof(buff[i]));
    


for ( j = 0; j < 32; j++ )

    {
        

char byte_1 = buff[0][0];
char byte_2 = buff[0][1];
char byte_3 = buff[0][2];
char byte_4 = buff[0][3];
char byte_5 = buff[0][4];
char byte_6 = buff[0][5];
char byte_7 = buff[0][6];
char byte_8 = buff[0][7];
char byte_9 = buff[0][8];
char byte_10 = buff[0][9];
char byte_11 = buff[0][10];
char byte_12 = buff[0][11];
char byte_13 = buff[0][12];
char byte_14 = buff[0][13];
char byte_15 = buff[0][14];
char byte_16 = buff[0][15];
char byte_17 = buff[0][16];
char byte_18 = buff[0][17];
char byte_19 = buff[0][18];
char byte_20 = buff[0][19];
char byte_21 = buff[0][20];
char byte_22 = buff[0][21];
char byte_23 = buff[0][22];
char byte_24 = buff[0][23];
char byte_25 = buff[0][24];
char byte_26 = buff[0][25];
char byte_27 = buff[0][26];
char byte_28 = buff[0][27];
char byte_29 = buff[0][28];
char byte_30 = buff[0][29];
char byte_31 = buff[0][30];
char byte_32 = buff[0][31];    

std::cout.setf(std::ios::hex, std::ios::basefield);
std::cout<< (int)byte_1 << std::endl;                 //printing a test to see if it works



        }


}

There's only 1 cout in the snippet posted, but it's going to repeat the value of byte_1 as an int 532 * 32 times. Since you are hardcoding the byte names eliminate the inner loop and change all the buff[x] where i is the i from the outer loop and x is as you have it.

i thought i needed the inner loop in order to process the blocks of 32 bytes? im not totally sure i understand you

Well it looks to me like you are telling it to print out the same thing about 16,000 times. If you want to change it to print out more of the entire array rather than just buff[0][0] of the array16,000 times, change these lines:

char byte_1 = buff[0][0];
char byte_2 = buff[0][1];
char byte_3 = buff[0][2];
char byte_4 = buff[0][3];
char byte_5 = buff[0][4];
char byte_6 = buff[0][5];
char byte_7 = buff[0][6];
char byte_8 = buff[0][7];
char byte_9 = buff[0][8];
char byte_10 = buff[0][9];
char byte_11 = buff[0][10];
char byte_12 = buff[0][11];
char byte_13 = buff[0][12];
char byte_14 = buff[0][13];
char byte_15 = buff[0][14];
char byte_16 = buff[0][15];
char byte_17 = buff[0][16];
char byte_18 = buff[0][17];
char byte_19 = buff[0][18];
char byte_20 = buff[0][19];
char byte_21 = buff[0][20];
char byte_22 = buff[0][21];
char byte_23 = buff[0][22];
char byte_24 = buff[0][23];
char byte_25 = buff[0][24];
char byte_26 = buff[0][25];
char byte_27 = buff[0][26];
char byte_28 = buff[0][27];
char byte_29 = buff[0][28];
char byte_30 = buff[0][29];
char byte_31 = buff[0][30];
char byte_32 = buff[0][31];

You are copying the same things into byte_1 through byte_32 512 times. Consider changing '0' to 'i'. That will get you different displays rather than the same thing over and over again. Right now you are simply outputting byte_1, which is always buff[0][0], about 16,000 times. You actually don't need byte_1 through byte_32 to display the values though. You can simply output buff[j].

If you are trying to simply display one element (buff[0][0]) at the end, you can take the "cout" statement outside of the loop. Right now it is inside the loop. However, if you do that, since you define byte_1 inside the loop, it will go out of scope before printing and give you an error if you try to use


cout << byte_1 << endl;

outside of the loop where you defined "byte_1".

If you want to simply display buff[0][0] once at the end, I would simply change that last line to :

cout << buff[0][0] << endl;

Keep the "std" qualifier if you didn't put "using namespace std" at the top. Not sure exactly what you're trying to do with printing statement so I can't give you any more advice, but the reason it's printing the same thing over and over again is because you've put the cout inside a doubly nested loop.

This article has been dead for over six months. Start a new discussion instead.