I have a file that contains integers organized into rows and separated by whitespace. Both the number and length of the rows are unknown.
I'm currently iterating over the file line-by-line, and I'm trying to iterate over each line character-by-character but I'm having some trouble. I'm currently storing the contents of each line into a string, but I suspect that's not the best way, and I'm hoping someone can point me in the right direction.
Here's my current code which simply prints each line in the file:
std::string filename = "values.txt";
std::ifstream file(filename.c_str());
std::string line;
while (std::getline(file, line))
{
std::cout << line << std::endl;
}
I'm coming from python, which could easily implement this, like so:
for line in file:
for char in line:
print char
However, not knowing the length/number of rows for the for loops is throwing me off as I'm fairly new to c++. I'd also like to read the characters as ints instead of a string, but I haven't been able to figure that one out either. What would be the most correct way to implement this in c++?
Any help is greatly appreciated.