Thread: writing to binary

  1. #16
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    Quote Originally Posted by simpleid View Post
    Code:
    for(;;)
    gross!
    Code:
    while (1)
    isn't beautiful either, but every loop type in C/C++ requires some type of loop condition, even if it's always true (presumably it's not worth having a separate type for an infinite loop since the compiler can recognize one when it sees it).

  2. #17
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    I'm not saying anyone is wrong? Or that you should do it any other way... I still find the syntax of "for(;; )" very visually awkward. I'm only expressing distaste in the style, despite the fact that you deem it preferred.

    Do what you want, but if complaints never arise, then change won't occur. Maybe in future languages the aspect will be addressed because someone like me said "damn that's ugly lookin fosho."

    :-)

    I agree though, while(1) has a very... odd appearance, the 1 is just as unnecessary as the ';;' should be... why can't 'true' be assumed?

    What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide the condition to break free within... that seems funny to me.
    Last edited by simpleid; 08-16-2007 at 12:48 PM.

  3. #18
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by simpleid View Post
    I agree though, while(1) has a very... odd appearance, the 1 is just as unnecessary as the ';;' should be... why can't 'true' be assumed?
    It is assumed on for-loops, hence the "for(;" works. I've seen people us a macro (e.g. "forever" to do "for(;".
    What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide it within.
    I usually try to use a condition in my while/for-loops - and in the above case, I suppose I should have used a "do ... while(fwrite(...) > 0);" instead.

    --
    Mats

  4. #19
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by simpleid View Post
    I'm only expressing distaste in the style, despite the fact that you deem it preferred.
    I didn't say it was preferred -- I said it was typical. I've seen and used both.

    Heck, if you want:

    Code:
    #define forever for(;;)
    
    forever
    {
        ...
    }
    What I find as ironic, is that the two most popular looping methods provide a mechanism to break out, yet we ignore it and provide the condition to break free within... that seems funny to me.
    Sometimes the natural place to break the loop is not at the beginning or end. Contorting your code so that the break is triggered by the loop condition can sometimes result in a big mess that's hard to figure out. I'm not making any judgement either way about this PARTICULAR case, since I haven't bothered looking at it in detail.

  5. #20
    Registered User
    Join Date
    Jul 2006
    Posts
    162
    oh i know, i just wanted to try and make a joke out of it. heh.

    however, the 'forever' really does look clean... might have to start using that...

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    835
    > however, the 'forever' really does look clean... might have to start using that...
    I wouldn't do that. It's only slightly shorter, and evil, being a macro. If forever is ever made a built-in keyword, that would be different.

    Edit: The exact same length, actually, assuming no white space in for(;;). But someone seeing the latter doesn't have to hunt around to verify that it means what they think it does.
    Last edited by robatino; 08-16-2007 at 02:45 PM.

  7. #22
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    I'm not making any judgement either way about this PARTICULAR case, since I haven't bothered looking at it in detail.
    Without looking at it much in detail, I daresay the example in question:
    Code:
    for(;;) {
      for(i = 0; i < 1000; i++)
          buf[i] = n+i;
      n += 1000;
      if (fwrite(f, buf, sizeof(buf), 1) <= 0)
        break;
    }
    Can be just as easily expressed as:
    Code:
    do {
      for(i = 0; i < 1000; i++)
          buf[i] = n+i;
      n += 1000;
    } while (fwrite(f, buf, sizeof(buf), 1) > 0);
    Thus sidestepping this "which deliberate infinite loop is better?" argument, but creating a "is a do while loop better than a deliberate infinite loop where both will suffice?" argument
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Writing std::string in binary mode
    By VirtualAce in forum C++ Programming
    Replies: 16
    Last Post: 11-06-2010, 07:39 PM
  2. Writing a struct to a binary file
    By Scarvenger in forum C++ Programming
    Replies: 8
    Last Post: 09-12-2006, 01:50 AM
  3. Writing binary data to a file (bits).
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 03:53 PM
  4. binary search and search using binary tree
    By Micko in forum C++ Programming
    Replies: 9
    Last Post: 03-18-2004, 10:18 AM
  5. binary to decimal
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 03-14-2004, 08:35 PM