Like Tree1Likes

Problem with a for() loop.

This is a discussion on Problem with a for() loop. within the C Programming forums, part of the General Programming Boards category; Code: if(byte1==0xDF && byte2==0x00 && byte3==0x00 && byte4==0x00 && byte5==0x00 && byte6==0x00 && byte7==0x00 && byte8==0x00) { break; } A ...

  1. #31
    Registered User
    Join Date
    May 2009
    Posts
    2,543
    Code:
    if(byte1==0xDF && byte2==0x00 && byte3==0x00 && byte4==0x00 && byte5==0x00 && byte6==0x00 && byte7==0x00 && byte8==0x00) { break; }
    A poor way to handle EOF with only minor changes to your code.

    Code:
    if(byte1==EOF || byte2==EOF || byte3==EOF || byte4==EOF || byte5==EOF || byte6==EOF || byte7==EOF || byte8==EOF) { 
        byte1=0xDF;
        byte2=0x00;
        byte3=0x00;
        byte4=0x00;
        byte5=0x00;
        byte6=0x00;
        byte7=0x00;
        byte8=0x00;     
    }
    
    if(byte1==0xDF && byte2==0x00 && byte3==0x00 && byte4==0x00 && byte5==0x00 && byte6==0x00 && byte7==0x00 && byte8==0x00) { break; }
    Not tested or syntax checked.

    Tim S.

  2. #32
    Member
    Join Date
    Mar 2011
    Posts
    40
    Salem:
    I'll post the general bits for file reading here for you. If you want the whole source code, I can create a PasteBin entry for you.

    Code:
    char portfromentry[145],porttoentry[145];
    int dltoport,dltoreplace,extradl,ofstoput,ofstoputstatic,trianglestoget,conversiontheorem,currentcommandoffset,getvertex,writevertex,currentround=0,twentyfour,area,texturelength,grabbed,extradlcheck;
    FILE *portfrom, *portto, *vertexdata, *texturedata, *displaylist;
    
    printf("\nZOBJ to port from: ");
    scanf("%s",portfromentry);
    printf("\nZOBJ to port to: ");
    scanf("%s",porttoentry);
    printf("\nDisplay List to port: 0x");
    scanf("%X",&dltoport);
    printf("\nDisplay List to replace: 0x");
    scanf("%X",&dltoreplace);
    
    portfrom=fopen(portfromentry,"r+b");
    portto=fopen(porttoentry,"r+b");
    displaylist=fopen("temp1.zmuffin","w+b");
    vertexdata=fopen("temp2.zmuffin","w+b");
    texturedata=fopen("temp3.zmuffin","w+b");
    
    for( ; ; )
    {
        byte1=fgetc(portfrom);
        byte2=fgetc(portfrom);
        byte3=fgetc(portfrom);
        byte4=fgetc(portfrom);
        byte5=fgetc(portfrom);
        byte6=fgetc(portfrom);
        byte7=fgetc(portfrom);
        byte8=fgetc(portfrom);
    
        fputc(byte1,displaylist);
        fputc(byte2,displaylist);
        fputc(byte3,displaylist);
        fputc(byte4,displaylist);
        fputc(byte5,displaylist);
        fputc(byte6,displaylist);
        fputc(byte7,displaylist);
        fputc(byte8,displaylist);
    
        if(byte1==0xDF) { break; }
    }
    Even though it all works so flawlessly for me, Windows gets stuck in a permanent loop when dealing with the loop unless it's working with extremely small Display Lists (that have a DF Command 0x50 or less bytes from the start). After reading and putting so many times, it beings to fail to read bytes the way it's supposed to, so by the time it gets to a DF Command about 0x200 bytes later, it doesn't notice that byte1==0xDF, so it never breaks out of the loop. I again repeat that it works flawlessly for me, but Windows struggles with it.

    With the actual problem better explained now, hopefully a solution would be easier to come across.




    Stahta01:
    Actually, the DF Command isn't always at the end of the file that we're porting from. For example, if the user inputs 0x6A48 as the Display List to port, which is, say, 0x528 Bytes long (in this example only), and the total file-size is 0x7FF8, it would be separating the Display List only, which would start at 0x6A48 and end at the end of the DF Command that is up next. It's kind of difficult to explain, despite how simple it really is.
    Last edited by Flotonic; 06-07-2011 at 01:42 PM.

  3. #33
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Flotonic View Post
    I again repeat that it works flawlessly for me, but Windows struggles with it.
    Translation: "Some guy says it doesn't work, but I can't repeat his effort to break it."

    Good job wasting everyone's time with something you aren't actually experiencing.
    Code:
    while( fread( buf, 1, 8, fp ) == 8 )
    {
        if( buf[ 0 ] == stopbyte )
            break;
        else /* optionally remove the else statements and just have it do this anyway */
        if( buf[ 1 ] == stopbyte || buf[ 2 ] == stopbyte ||
            buf[ 3 ] == stopbyte || buf[ 4 ] == stopbyte ||
            buf[ 5 ] == stopbyte || buf[ 6 ] == stopbyte ||
            buf[ 7 ] == stopbyte )
            printf( "stopbyte found out of position\n" );
        else
            ...do whatever it is you are doing...
    }
    But like I said, troubleshooting some random guy's "problem" without actually duplicating it yourself, or actually having his broken files is well... sort of like this thread.


    Quzah.
    Hope is the first step on the road to disappointment.

  4. #34
    Member
    Join Date
    Mar 2011
    Posts
    40
    I optimized the code earlier today and am now getting the same permanent loop problem on Linux, same for Windows. The good part is that the data ripped from the file on each system is one hundred percent identical. Pretty much, my only remaining problem is getting it to break out of the loop once a DF Command is found. The code is as follows.

    Code:
        for( ; ; )
        {
            for(fill=0;fill<9;fill+=1) {
                bytegrabber=fgetc(portfrom);
                bytestoring[fill]=bytegrabber; }
    
            for(fill=0;fill<9;fill+=1) {
                bytegrabber=bytestoring[fill];
                fputc(bytegrabber,displaylist); }
    
            if(bytestoring[1]==0xDF && bytestoring[2]==0x00 && bytestoring[3]==0x00 && bytestoring[4]==0x00 && bytestoring[5]==0x00 && bytestoring[6]==0x00 && bytestoring[7]==0x00 && bytestoring[8]==0x00) { break; }
        }
    The solution here must be obvious, even though I'm not seeing it. I've tried shifting all of the variables down (starting at bytestoring[0] instead of bytestoring[1]) as well as checking bytestoring[0] or bytestoring[1] by itself and not alongside its family. Does anyone mind enlightening me and telling me what I'm doing wrong while trying to break out of this? Thanks in advance!

  5. #35
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you reading nine bytes instead of eight now? Why aren't you just using fread?


    Quzah.
    Last edited by quzah; 06-09-2011 at 04:30 PM.
    Hope is the first step on the road to disappointment.

  6. #36
    Member
    Join Date
    Mar 2011
    Posts
    40
    Ohh. Right. The problem is now fixed. I didn't try both changes in conjunction. Again, a simple answer. Thanks a lot!

    How might I lock a topic?

  7. #37
    Registered User
    Join Date
    Mar 2011
    Posts
    261
    Quote Originally Posted by Flotonic View Post
    How might I lock a topic?
    That's not for you to decide. ;-)

  8. #38
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    There is no "close" or "solved" tag on this forum, treads are simply abandoned when the OP is happy with the result (or quietly leaves never to be heard from).

    It is rare that someone will bump it several years later with some "me too" refrain, and rarer still that they might have something useful to contribute.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  9. #39
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,488
    However, this one has gone on long enough, and is wandering into forum meta discussion. If it really interests you, try asking in the GD forum.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Page 3 of 3 FirstFirst 123
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem with loop.
    By Squish in forum C++ Programming
    Replies: 2
    Last Post: 10-12-2010, 09:21 AM
  2. loop problem
    By sababa.sababa in forum C Programming
    Replies: 6
    Last Post: 12-20-2009, 01:56 PM
  3. For Loop problem
    By lawzbhoy in forum C++ Programming
    Replies: 4
    Last Post: 05-14-2008, 11:39 AM
  4. for loop problem
    By Makoy in forum C++ Programming
    Replies: 1
    Last Post: 12-25-2004, 09:08 AM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21