Thread: Problem with a for() loop.

  1. #1
    Member
    Join Date
    Mar 2011
    Posts
    40

    Problem with a for() loop.

    Currently, in Linux, the for() loop I've written gives me no problems. However, when Windows users try working with it, the data get offset after so many times going around and they get thrown into an infinite loop. I'll show the for() loop, the data that a Windows user gets, and the data that I get from it.

    The for() loop:
    Code:
        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 && byte2==0x00 && byte3==0x00 && byte4==0x00 && byte5==0x00 && byte6==0x00 && byte7==0x00 && byte8==0x00) { break; }
        }
    Windows User Data (this is cut off; the FFs keep being generated for infinity):
    Code:
    D7 00 00 02 03 E8 01 F4 E7 00 00 00 00 00 00 00
    FC 12 16 04 5F FD FF F8 FA 00 00 FF 80 80 80 FF
    FB 00 00 00 00 14 50 FF E2 00 00 1C C8 11 20 78
    E3 00 10 01 00 00 00 00 FD 90 00 00 06 00 0B 00
    F5 90 00 00 07 05 01 41 E6 00 00 00 00 00 00 00
    F3 00 00 00 07 07 F4 00 E7 00 00 00 00 00 00 00
    F5 88 04 00 00 05 01 41 F2 00 00 00 00 03 C0 3C
    D9 00 00 00 00 2F 00 05 01 02 00 40 06 00 7D 28
    06 00 02 04 00 04 02 06 06 04 08 0D 0A 00 06 08
    04 06 0C 0E 10 00 0E 12 10 06 0E 14 12 00 0E 16
    14 06 0E 18 FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF FF
    FF FF FF FF FF FF FF FF
    Linux Data (actual data):
    Code:
    D7 00 00 02 03 E8 01 F4 E7 00 00 00 00 00 00 00
    FC 12 16 04 5F FD FF F8 FA 00 00 FF 80 80 80 FF
    FB 00 00 00 00 14 50 FF E2 00 00 1C C8 11 20 78
    E3 00 10 01 00 00 00 00 FD 90 00 00 06 00 0B 00
    F5 90 00 00 07 05 01 41 E6 00 00 00 00 00 00 00
    F3 00 00 00 07 07 F4 00 E7 00 00 00 00 00 00 00
    F5 88 04 00 00 05 01 41 F2 00 00 00 00 03 C0 3C
    D9 00 00 00 00 2F 00 05 01 02 00 40 06 00 7D 28
    06 00 02 04 00 04 02 06 06 04 08 0A 00 06 08 04
    06 0C 0E 10 00 0E 12 10 06 0E 14 12 00 0E 16 14
    06 0E 18 1A 00 1C 0E 1A 06 0E 1E 18 00 0E 0C 1E
    06 1E 20 18 00 1E 22 20 06 22 24 20 00 22 26 24
    06 26 28 24 00 28 2A 24 06 2C 2E 26 00 2C 30 2E
    06 2A 32 24 00 34 14 36 06 14 16 36 00 10 36 0C
    06 10 34 36 00 2C 32 30 06 38 2C 22 00 2C 26 22
    06 3A 38 1E 00 38 22 1E 06 3C 3A 36 00 3C 36 3E
    06 3A 1E 36 00 1E 0C 36 D9 00 00 00 00 2F 04 05
    06 10 12 14 00 10 14 34 E7 00 00 00 00 00 00 00
    FB 00 00 00 8A 14 5A FF FD 90 00 00 06 00 0B 00
    F5 90 00 00 07 05 01 40 E6 00 00 00 00 00 00 00
    F3 00 00 00 07 07 F4 00 E7 00 00 00 00 00 00 00
    F5 88 04 00 00 05 01 40 F2 00 00 00 00 03 C0 3C
    D9 00 00 00 00 2F 00 05 01 01 40 28 06 00 7B E8
    06 00 02 04 00 00 06 02 06 08 06 00 00 0A 08 00
    06 0C 0E 10 00 0E 12 10 06 0E 14 12 00 14 16 12
    06 14 18 16 00 18 1A 16 06 12 1C 1E 00 12 16 1C
    06 16 20 1C 00 16 1A 20 06 10 12 1E 00 10 1E 0C
    06 18 22 1A 00 18 24 22 06 1A 26 20 00 26 24 20
    DF 00 00 00 00 00 00 00
    Is there a way to "free" variables each time during the loop that I don't know of? Thanks in advance!

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    Do you open the file in Binary Mode?
    This is not needed under Linux; but, required under Windows.

    Tim S.

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    40
    Code:
    displaylist=fopen("temp1.zmuffin", "r+");
    So I don't think I did. Mind elaborating about this "binary mode?"

    EDIT:

    Now binary mode is used. Example:
    Code:
    portfrom=fopen(portfromentry,"r+b");
    However, the problem for Windows people still persists, but it's now closer than before. Any ideas now?

    Code:
    D7 00 00 02 03 E8 01 F4 E7 00 00 00 00 00 00 00
    FC 12 16 04 5F FD FF F8 FA 00 00 FF 80 80 80 FF
    FB 00 00 00 00 14 50 FF E2 00 00 1C C8 11 20 78
    E3 00 10 01 00 00 00 00 FD 90 00 00 06 00 0B 00
    F5 90 00 00 07 05 01 41 E6 00 00 00 00 00 00 00
    F3 00 00 00 07 07 F4 00 E7 00 00 00 00 00 00 00
    F5 88 04 00 00 05 01 41 F2 00 00 00 00 03 C0 3C
    D9 00 00 00 00 2F 00 05 01 02 00 40 06 00 7D 28
    06 00 02 04 00 04 02 06 06 04 08 0D 0A 00 06 08
    04 06 0C 0E 10 00 0E 12 10 06 0E 14 12 00 0E 16
    14 06 0E 18 1A 00 1C 0E 1A 06 0E 1E 18 00 0E 0C
    1E 06 1E 20 18 00 1E 22 20 06 22 24 20 00 22 26
    24 06 26 28 24 00 28 2A 24 06 2C 2E 26 00 2C 30
    2E 06 2A 32 24 00 34 14 36 06 14 16 36 00 10 36
    0C 06 10 34 36 00 2C 32 30 06 38 2C 22 00 2C 26
    22 06 3A 38 1E 00 38 22 1E 06 3C 3A 36 00 3C 36
    3E 06 3A 1E 36 00 1E 0C 36 D9 00 00 00 00 2F 04
    05 06 10 12 14 00 10 14 34 E7 00 00 00 00 00 00
    00 FB 00 00 00 8A 14 5A FF FD 90 00 00 06 00 0B
    00 F5 90 00 00 07 05 01 40 E6 00 00 00 00 00 00
    00 F3 00 00 00 07 07 F4 00 E7 00 00 00 00 00 00
    00 F5 88 04 00 00 05 01 40 F2 00 00 00 00 03 C0
    3C D9 00 00 00 00 2F 00 05 01 01 40 28 06 00 7B
    E8 06 00 02 04 00 00 06 02 06 08 06 00 00 0D 0A
    08 00 06 0C 0E 10 00 0E 12 10 06 0E 14 12 00 14
    16 12 06 14 18 16 00 18 1A 16 06 12 1C 1E 00 12
    16 1C 06 16 20 1C 00 16 1A 20 06 10 12 1E 00 10
    1E 0C 06 18 22 1A 00 18 24 22 06 1A 26 20 00 26
    24 20 DF 00 00 00 00 00 00 00
    Now the data is closer to what it should be, but there are some unreasonable shifts and other things. Thanks for you time. Any other ideas?
    Last edited by Flotonic; 06-04-2011 at 05:57 PM.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    If I were to guess, since I don't feel like reading hundreds of data points to see where it's different, I'd say it's this: Newline - Wikipedia, the free encyclopedia


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

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > However, the problem for Windows people still persists, but it's now closer than before. Any ideas now?
    So how did you get your Linux data onto your windows box?

    > 06 00 02 04 00 04 02 06 06 04 08 0D 0A 00 06 08
    Because it seems to me that 0A got converted into 0D 0A by whatever you used to transfer it.
    If you used FTP, you need to make sure that was in binary mode as well.

    Begin by checking the actual file sizes down to the number of bytes.

    Then perhaps use things like md5sum to make sure the file still has the same contents on both machines (the checksum is identical).
    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.

  6. #6
    Member
    Join Date
    Mar 2011
    Posts
    40
    I'm referring to when executing the program itself. It copies a certain block of data from a file to a new file. (The program was put on Windows by compiling under Windows. Very simple.)

    After reading so many bytes, the data being written to the new file becomes offset, as seen in the previous code examples. I'll write some pseudo-data just to further define what's going on.

    Line_________Original_Data________Ripped_Data_____ ___Comparison
    Line_1_______57_87_97_67________57_87_97_67_______ _Successful!
    Line_2_______89_97_48_49_________99_97_48_49______ _Less accurate
    Line_3_______15_30_45_00_________89_40_45_00______ _Even less accurate.
    Line_4_______13_78_95_15_________45_61_17_15______ _Even less accurate.
    Line_5_______89_03_4A_19_________30_76_22_57______ _Zero accuracy.
    Line_6_______78_5A_7B_8B_________FF_FF_FF_FF______ __Writing NULL.

    After every so many time bytes are read by the program, they become less accurate. After reading so many bytes, NULL results are written to the file.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Flotonic View Post
    (The program was put on Windows by compiling under Windows. Very simple.)
    That tells us less than nothing about how the data file got there.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Flotonic View Post
    I'm referring to when executing the program itself. It copies a certain block of data from a file to a new file. (The program was put on Windows by compiling under Windows. Very simple.)

    After reading so many bytes, the data being written to the new file becomes offset, as seen in the previous code examples. I'll write some pseudo-data just to further define what's going on.

    Line_________Original_Data________Ripped_Data_____ ___Comparison
    Line_1_______57_87_97_67________57_87_97_67_______ _Successful!
    Line_2_______89_97_48_49_________99_97_48_49______ _Less accurate
    Line_3_______15_30_45_00_________89_40_45_00______ _Even less accurate.
    Line_4_______13_78_95_15_________45_61_17_15______ _Even less accurate.
    Line_5_______89_03_4A_19_________30_76_22_57______ _Zero accuracy.
    Line_6_______78_5A_7B_8B_________FF_FF_FF_FF______ __Writing NULL.

    After every so many time bytes are read by the program, they become less accurate. After reading so many bytes, NULL results are written to the file.
    Where did you get that "pseudo-data" from? I can't find it anywhere in the data you provided. Also, comparing the two program's output; it seems that everyone is right, the first difference (according to Linux's diff program) is in the following line:
    Code:
    < 06 00 02 04 00 04 02 06 06 04 08 0A 00 06 08 04
    > 06 00 02 04 00 04 02 06 06 04 08 0D 0A 00 06 08
    And as several people stated before, the only change is that Windows uses '\r\n' as a newline and Linux '\n'. So with the information you provided us so far, there's nothing anybody here can do for you, if there is an error still at all.

  9. #9
    Member
    Join Date
    Mar 2011
    Posts
    40
    "Pseudo" means fake. That was just fake bytes that I threw together in order to better illustrate the problem and in a smaller amount of space.

    The data file gets there when the program is ran. It asks for a file to grab data from and the offset to begin grabbing data. For example:

    File to grab data from: [USER INPUT.]
    Offset to begin grabbing data: [USER INPUT.]

    It goes to the offset relative to the start of the file and grabs bytes, writing them to a new file as it goes, until it runs into a certain byte. The problem is that when this program is ran as a Windows user, the data becomes offset after so many loops. I'm looking for something cross platform, and the code currently only executes the way it is supposed to when running on Linux.

  10. #10
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I've already told you what your problem is.

    Ascii Table - ASCII character codes and html, octal, hex and decimal chart conversion

    Go there and be surprised and amazed at what 0D and 0A are, and then run along.


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

  11. #11
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    You've yet to prove to us that the data file you copied from Linux to Windows is undamaged by your attempt to copy it.

    If your Linux file contains an 0A, which is transformed to 0D 0A on Windows, then you need to look at the copying process, not your code.
    There is nothing you can do in your code to fix this.
    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.

  12. #12
    Member
    Join Date
    Mar 2011
    Posts
    40
    Ohh! Now I see it. :P Thank ya! The problem now is modifying the code or doing something so Windows doesn't do said conversions. Because I'm still relatively new and still, in fact, have the right-out-of-the-box smell, have any ideas for how to fix this?

    Proof that the data reads correctly on Linux is because it doesn't enter a permanent loop, the data stops cleanly at a DF Command, and all data is grabbed successfully. A sword wouldn't have been successfully ported from one file to another (game-wise) had the code had a flaw in it. Link.

    A custom .ini file or anything to get Windows to go into a different mode when reading code?

  13. #13
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    OK, last time of asking - is your data file the SAME on both systems?

    If the answer is NO, then you are screwed. There is no fix you can apply in your program code where you can automatically patch 0D 0A pairs, because there is always the random chance that 0D 0A might appear in a normal data file anyway.
    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.

  14. #14
    Member
    Join Date
    Mar 2011
    Posts
    40
    Both the source code and the files being read/written to are the same on both systems. Yes. There shouldn't be a reason there can't be some sort of repair, though.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    Quote Originally Posted by Flotonic View Post
    Both the source code and the files being read/written to are the same on both systems. Yes. There shouldn't be a reason there can't be some sort of repair, though.
    And here we go again. No, the files are not the same on both Windows and Linux, I'm quite sure. You haven't even answered half of the questions that will lead to an answer, and you even throw in random "fake" data that only puts anyone reading it off - because it definitely does NOT show what is wrong here.

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, 02: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, 10:08 AM
  5. While loop problem
    By nadkingcole in forum C Programming
    Replies: 2
    Last Post: 05-09-2002, 06:14 AM