35 36 37 38 39 40
|
// Read the actual data into the vector
while (datafile >> hexvalue && hexvalue[0] == '0')
{
long n = strtol(hexvalue.c_str(), endptr, 0 );
data1.push_back(n);
}
|
It's quite common to read something from an input stream, and verify that the read was successful, like this:
1 2 3 4
|
while (datafile>>hexvalue)
{
// do something with the item which was read
}
|
Here, this part does the actual input from the file,
datafile>>hexvalue
To test whether the input was successful, we could then test the state of the input stream, like this,
if (datafile.good()), but there is an abbreviated version, where we can do both the input and the check in one line, like this:
if (datafile>>hexvalue)
All of that is pretty much a typical usage. The next part is specific to this program. We are interested in the individual items of data, like this:
0xff,, so this test
hexvalue[0] == '0' is a basic verification that the first character is a zero. Eventually the item read will be the closing brace "}" and that test will give
false, thus ending the loop.
So to clarify, perhaps extra parentheses will make the meaning clearer, and don't do any harm.
35 36 37 38 39 40
|
// Read the actual data into the vector
while ( (datafile >> hexvalue) && (hexvalue[0] == '0') )
{
long n = strtol(hexvalue.c_str(), endptr, 0 );
data1.push_back(n);
}
|
One more thing. We are interested in this "0xff" but are actually getting this "0xff," with a trailing comma. In this case it doesn't matter, as the function
strtol() will stop at the first character which isn't part of a valid number.
Finally, I'm assuming the format of the input file doesn't have any extra spaces. That is, I expect this
0xff, 0xdd, 0x21, rather than this
0xff , 0xdd , 0x21 ,. The second version is valid c code, but would require a small adjustment to the code I gave or it will halt at the first comma.