You'd need a loop that reads the file line by line, does stuff, and moves on down the row:
1 2 3 4 5 6 7 8 9 10 11 12
|
#include <fstream>
#include <string>
#include <iostream>
int main()
{
std::ifstream f("filename.txt"); // <- your filename here
std::string line; // we'll store each line here
while(getline(f, line)) // this is the loop that reads line by line
{
std::cout << line << '\n'; // do stuff here
}
}
|
If you run this, you will see that it prints out the lines. Always test the code as you write it.
Now, let's see what should the "do stuff here" line be replaced with: you have your line, such as "45 67 34 78 89" stored in the variable "line". You want those characters to become numbers, so you need to convert line from string to stream, and read numbers from the stream:
1 2 3 4 5 6 7 8
|
// "do stuff here" becomes
std::istringstream buf(line); // convert string to stream
int n; // we'll store the integers here
while(buf >> n)
{
std::cout << n << '\n';
}
std::cout << " done with the line! TODO: print min and max here\n";
|
again, compile and test to see that the numbers are printed. You will need an #include <sstream> at the beginning to use istringstream.
Now, you want min and max for each pass: you can remember the largest and the smallest integer seen in each loop... can you do that?
PS: Noting that you bring up arrays, arrays are not suitable for this task because you don't know the number of integers in the line beforehand. You could store the integers that are read from the istringstream into a vector and use the standard functions min_element() and max_element() (or minmax_element() if your compiler is recent) to obtain the results, but it's a waste of storage.