int main () {
string line;
ifstream myfile ("example.txt");
if (myfile.is_open())
{
while ( getline (myfile,line) )
{
cout << line << endl;
int lineNumber;
stringstream(line) >> lineNumber;
//Do smith with this line read amount of digits
int tempNumber = lineNumber;
num_digits = 0;
//get number of digits
while(tempNumber > 0) {
num_digits++;
tempNumber/=10;
}
//print digits
std::stringstream tmp_stream;
tmp_stream << tempNumber;
for (i = 0; i < tmp_stream.str().size(); i++)
{
std::cout << "Digit [" << i << "] is: " << tmp_stream.str().at(i) << std::endl;
}
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
//get number of digits
is some extra, maybe you will fond a better way of getting digits, so this could be useful
这完全是为了你的目的:
if (myfile.is_open()){
int line = 0;
while ( getline (myfile,line) )
{
cout << line << endl;
for (i = 0; i < line.size(); i++)
{
std::cout << "Digit [" << i << "] is: " << line.at(i) << std::endl;
int lineDigitNumber;
stringstream(line) >> lineDigitNumber;
}
line++;
//Fill array with line and i
}
myfile.close();
}