"Jus!" <jm****@bigpond.com> wrote in message
news:uM******************@news-server.bigpond.net.au...
The file contains 1's ad 0's but i need to use them as ints.
Each line contains a string of ints (ie. 100101)
And i need to break that line up into separate ints (1 0 0 1 0 1 all
separate)
I tried to no avail:
instream >> data;
for(j = 0; j < x; j++)
{
stringstream sstream(data[j]);
sstream >> x_value[i][j];
}
Oh, I see. I didn't realize that you wanted to read the individual bit
values. I don't understand the code you gave (mostly because it was
incomplete), but it shouldn't be too difficult to process each bit value
individually:
#include <fstream>
#include <string>
std::ifstream file("file.txt");
if ( !file ) {
// Error opening file. Complain.
}
else {
std::string line;
while( std::getline(file, line) ) {
// Individually process every character on the line
for ( std::string::iterator it = line.begin(); it != line.end();
++it ) {
char c = *it;
if ( c != '0' && c != '1' ) {
// The character read was not a zero or one.
// Complain about invalid input.
}
else {
int num = c - '0';
// Do something with the zero or one stored in "num"
}
}
}
if ( !file.eof() ) {
// Not all of the lines in the file were read. Complain.
}
}
--
David Hilsee