can't break out of a while loop?

greets

so I'm trying to use a - if then break - statement in a while loop and it does not break out of the loop.

        while (myFile.available()) {
          Serial.write(myFile.read());
          delay(100);
          char peek_char = myFile.peek();
          if ((peek_char) == 8){
            break;
          }          
        }

it's using the SD.h library.

The peek function returns a char/byte. I tested it with Serial.print and it works fine, however when myFile.peek() returns an "8" the while loop should break but it doesn't, am I forgetting something?

thanks

...however when myFile.peek() returns an "8"...

An '8' or an 8?

If you're reading a text file and are looking for the symbol 8, you'll want to do:

if ((peek_char) == '8')

instead. Note the presence of quotes - this tells the compiler you're comparing against a symbol 8, not the number 8 (which as a character in a text file is a non-printing character (that is, it isn't associated with a number or character as you and I think of them) ).

MysteriousAges:
If you're reading a text file and are looking for the symbol 8, you'll want to do:

if ((peek_char) == '8')

instead. Note the presence of quotes - this tells the compiler you're comparing against a symbol 8, not the number 8 (which as a character in a text file is a non-printing character (that is, it isn't associated with a number or character as you and I think of them) ).

Thanks! I knew it was something simple, I was actually trying "8" but of course it did not work.

an off-top Q:

How would I transform a '8' into an 8, i.e. a byte or integer so I could do mathematical operations with it?

Check out the String library reference for toInt: toInt() - Arduino Reference

You'll want to be careful and do check for bad data when converting. :wink:

naut:
How would I transform a '8' into an 8, i.e. a byte or integer so I could do mathematical operations with it?

peek_char - '0'

MysteriousAges:
Check out the String library reference for toInt: toInt() - Arduino Reference

He made no mention of using Strings, and pointing him to the String library is just plain cruel.

The other thing to worry about, is that the read() function and the peek() function may be trying to read the next two characters of the file, and there may be only one character left. You may need to consider what will happen in that case.

lar3ry:

MysteriousAges:
Check out the String library reference for toInt: toInt() - Arduino Reference

He made no mention of using Strings, and pointing him to the String library is just plain cruel.

In what way is it cruel? I tried the toInt() function and it works fine.

hmm, you mean peek_char minus '0' or something else?

you mean peek_char minus '0'

Exactly that.

In what way is it cruel? I tried the toInt() function and it works fine.

Strings (as in the String object), are not suitable for use in a very small RAM space. C-style strings (char arrays) are better for a number of reasons. They are not copied every time they are changed, like Strings are. Because of this, Strings tend to fragment memory, and will almost invariably cause memory problems and a lot of time wasted on garbage collection. Strings would be fine to use if you have a megabyte or two, but a 2KByte RAM space (in an Uno, for example), is not the right place to use Strings.

hmm, you mean peek_char minus '0' or something else?

Yes, that's exactly what he means.

'8' is a character, which is a byte with a value of 0x38, or 56 decimal, or 00111000 binary.
'0' is a character, which is a byte with a value of 0x30, or 48 decimal, or 00110000 binary.

So to get a numeric value of the characters between '0' and '9', you can subtract '0' (or 0x30, 56 decimal)

'8' is a character, which is a byte with a value of 0x38, or 56 decimal, or 00111000 binary.
'0' is a character, which is a byte with a value of 0x30, or 48 decimal, or 00110000 binary.

So to get a numeric value of the characters between '0' and '9', you can subtract '0' (or 0x30, 56 decimal)

thanks, I'll try that. Btw, small correction, you wrote 56 instead of 48 decimal.

-style strings (char arrays) are better for a number of reasons. They are not copied every time they are changed, like Strings are.

you mean that if I have:

String stringNumber;

every-time this happens:

stringNumber +=  'a' ;

stringNumber is duplicated in RAM?

naut:
you mean that if I have:

String stringNumber;

every-time this happens:

stringNumber +=  'a' ;

stringNumber is duplicated in RAM?

Yes. The String is copied, and the memory that holds the old one is marked as free, which is to say that eventually it gets re-used. But the problem comes when you now add another character or String to it. The memory that was freed is now not big enough to hold the newest String (or perhaps not even large enough to hold an int or a long), so yet another chunk of memory is allocated. In this manner, your memory can become fragmented fairly quickly, and with only 2K bytes of RAM, you can't afford to use them. There are many other ways for Strings to mess you up. This is just one example.

I would say that C-style strings (char arrays) are only marginally more difficult to understand, but are not only faster when manipulated, but don't fragment your memory. Any C language tutorial will tell you how to use them.

lar3ry:
I would say that C-style strings (char arrays) are only marginally more difficult to understand, but are not only faster when manipulated, but don't fragment your memory. Any C language tutorial will tell you how to use them.

Yep, I spent a few hours yesterday learning about char arrays, they're not so difficult as it might seem, although not as convenient as Strings.

although not as convenient as Strings.

True. They are more convenient.