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;
}
//獲取數字 數是一些額外的,也許你會喜歡得到數字的更好的辦法,所以這可能是有用的
正是你的目的只是這需要:
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();
}
這樣嗎?有什麼問題? –
假設每個條目的長度只有一位數是否安全? – moshbear
@moshbear:是的 – Avinash