Thread: loops

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    99

    loops

    hi, im having a few problems implementing a loop into my program that will do exactly what im after. Firstly my loop has to run over 16384 bytes. This is the maximum and minimum it will run for. The problem is I need to create a loop that will break the 16384 bytes down into sections but each section will not always be the same size. For example the first time i run through my loop i mite need to take the first 32 bytes and preform all my processing on those bytes. Then when i go through again i might need to do my processing on the next 96 bytes and so on. Basically I need to process my loop so that it runs through all 16384 bytes but each section i process may differ in size (multiples of 32). Basically im parsing through the directory structure of a FAT image and Long files names are causing the difference in the section size of my loop. Ive been racking my brains on how to do this and I think the loop is going to be the best way but is it possible to create a loop that changes its chunk size depending on what i want (if that makes sense)?

    thanks

  2. #2
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    Code:
    int pos=0;
    int stop = 16384;
    while(pos<stop)
    {
        void *block;
        unsigned int blockSize = getNextBlock(block,pos);
        processBlock(block,blockSize);
        pos+=blockSize;
    }

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Something like
    Code:
    for ( i = 0 ; i < 0x4000 ; i += amount ) {
      // various things which update amount depending on what is found
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    99
    brill this looks like the sort of thing I was after! Can i just check i know how this works first to make sure I just dont jump into things head first. I'm not toally sure what is occuring inside the loop and how its happening but I think i know whats going on just not how its doing it.

    One thing is that the first byte of each block tells me how much the block size has to be, so for example i read the first byte, i work out that corresponds to 96 then i need to pass the 96 back to my loop somehow to say, i need 96 bytes and move my next chunk to start at the 97 byte to start the process all over again




    Code:
    int pos=0;                    //increments everytime it is passed a new block size
    int stop = 16384;      //max of my loop
    
    while(pos<stop)
    {
        
        void *block;                      //not sure what this does
    
        unsigned int blockSize = getNextBlock(block,pos);    //not sure what this does
    
        processBlock(block,blockSize);    //not sure what this does
    
        pos+=blockSize;   //Adding the block size to variable pos to increment its value?
    
    
    }

    thanks

  5. #5
    3735928559
    Join Date
    Mar 2008
    Location
    RTP
    Posts
    838
    that will not completely do everything you need.

    block is a pointer to a location within whatever object your are processing that has these variably sized blocks of information, or sections, as you were calling it. getNextBlock would be some function that finds the start of the next block/section from the location pos, sets block to point to that location, and returns the size of the block/section. it might be convenient to implement it as either whateverObjectYourProcessing::getNextBlock or getNextBlock(object,block,pos). i don't have enough background info to tell exactly which makes the best sense for you.

    process block would be a function that does whatever you're trying to do with these pieces of information.

    yes, incrementing pos by blocksize will make sure you stop when you hit 16384.

    so, i don't know what those functions do either because i don't know what you're doing. this is just a possible loop structure that might accomplish what you're after.
    Last edited by m37h0d; 08-10-2008 at 01:02 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Loops Trouble
    By rlframpton in forum C Programming
    Replies: 2
    Last Post: 04-17-2009, 01:08 AM
  2. Too many loops D:
    By F5 Tornado in forum C++ Programming
    Replies: 6
    Last Post: 12-03-2007, 01:18 AM
  3. help with arrays and loops
    By jdiazj1 in forum C Programming
    Replies: 4
    Last Post: 11-24-2001, 04:28 PM
  4. for loops in C
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 10-15-2001, 05:09 PM
  5. exiting loops with ease?
    By KingRuss in forum Game Programming
    Replies: 3
    Last Post: 09-24-2001, 08:46 PM