Get max value from numbers in text file

Hi folks.

I have a text file with a thousands of rows of numbers in it.
For example:

1 2 4 6 11 13 19 21 66 97 101 ........
1 3 9 12 18 26 44 56 98 113......
...........
......

What I want to do is find the maximum number that's in the file.
My thoughts are:

1 - the numbers are all strings, not integers, so I can't easily get the max val.
2 - do I need to use an array to store all the numbers in and calculate the max val from there?
3 - do I need to use itoa to convert the strings into integers?
4 - I need some way of making the program recognise a whole number (i.e. read in the data until you get to a space and then it will know its got a whole number).

Can anyone help me? Is there some fairly straight forward code I can use to do this?
Thanks
1. If the format of the file is well-behaved and you don't need to check for errors,
simply use an ifstream and read into integers. This will do the string-to-int conversion
for you.

2. No. Keep track of the maximum value read as you read each integer.

3. Not if you use ifstream and read integers, otherwise there are a plethora of ways
to convert strings to integers, itoa being perhaps the worst of them. See:
http://www.cplusplus.com/articles/numb_to_text/

4. ifstream does this for you.
Well c++ can do most of these things without you doing anything special.

First you create and open your file:

ifstream myfile("path/to/your/file.txt");

then you can read from it with operator >>

1
2
int myint;
myfile>>myint;


Note that this will read up to the next whitespace (space, tab, newline) and it will store whatever it finds and looks like an integer as an int. So you will neither have to convert strings to integers nor look manually for whitespace.

Since you want to read every number from the while, you will need to put the above code inside a loop that looks for the end of file to exit. The loop will look something like this.

1
2
3
4
while (!myfile.eof()) {
    myfile>>myint;
    //do whatever you want to do with myint
}


As for finding the largest number, you don't need to keep everyone in memory. Store the first number in a variable and if you find a larger number while you loop through the file replace the first one with it. When the loop finishes that variable will be the max.
Thanks for your reply.

I've tried:

int Val;
int maxVal;

while(!datFile.eof() )
{
datFile >> Val;
maxVal = Val;
if Val > maxVal
maxVal = Val;
}

but get an error (error C2061: syntax error : identifier 'Val'
whoops! forgot to add brackets around Val > maxVal
but it's not finding the largest number. It's returning "2" which is definately not the largest number.
Incidentally, "2" occurs on the second line, so could this be where "2" is coming from?
Ok I've got it to work.
But I have another problem.
I'm also trying to calculate the number of rows in the file.
I got that working already, but now if I try to calculate both the number of rows and the largest value in the same while statement, it doesn't calculate the max value for some reason.

My code is:

while(!datFile.eof() )
{
datFile >> Val;
n = Val;
if (Val > n)
n = Val;
getline(datFile, line);
m++; 'm is the int which holds the row numbers
}

Each ifstream instance has a pointer pointing to the next element to be read. The pointer is called the get pointer and when you open a file it point at the first element (unless you specify otherwise).

When you use operator >> ifstream reads until the next whitespace and the get pointer is set to the next element. When you use getline() ifstream reads until the next newline and again sets the get pointer to the next element.

In your case, the first time you use >> you read the first number and the get pointer points at the second. Then you use getline(), you read until the end of the line and the get pointer points at the first element of the second line. So, when you use >> for the second time you read the element pointed, which is the first number of the second number. As the loop runs, you only compare the first element of each line.

The easiest solution would be to loop once to find the max, set the get pointer to point at the beginning and then loop once more to find the number of rows. You can set the get pointer by calling myfile.setg(pos) where pos is the position you want the pointer to point to (0 for the beginning of the file).

Of course this way you would have to read the file twice. A better way would be to only use getline() to read from the file, counting the lines in the process, and each time extract the number from the string to find the max.

You will probably not see any difference in execution time, unless your file is several megabytes, but if you are interesting in programming it's important to start using best practices early.
It's ok, I've sorted it. Thanks for your help.
Topic archived. No new replies allowed.