Thread: Read max value from strings of numbers in text file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Read max value from strings of 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?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It sounds straightforward: use the overloaded operator>> for std::istream to read into an int variable in a loop. Keep track of the largest value.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    431
    What do you mean, "the numbers are all strings"? The text file is just text. Why can't you read them in as ints? Post the first few lines of your text file.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  4. #4
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    I'm not familiar with the overload operator. I'm a beginner. How do I use it?

    All lines in the code are just numbers, nothing more. Like I put in my post.
    So first few lines could be:

    1 3 5 9 11 26 33 86 132 198
    1 3 9 12 26 33 49 59 65 112
    etc
    etc
    .......

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by james890
    I'm not familiar with the overload operator. I'm a beginner. How do I use it?
    I suggest that you read a beginners' book or tutorial. The use of the overloaded operator>> for input is usually in one of the early lessons. If you do not know how to use this, it is unlikely that you know how to open a file for input, but a beginners' book or tutorial should also cover that.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Ah, I think I know what you mean. I just wasn't aware it was called the overloaded operator. But it will not be able to differentiate between numbers will it, because it is just a text file. Don't I need to tell it to recognise a space as the end of a number?

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Nope.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by james890
    Don't I need to tell it to recognise a space as the end of a number?
    You can try a simpler example: read in two integers separated by whitespace (a space will do), sum them, then print the result.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  9. #9
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    Ok, I've tried this:

    Code:
    	while(!datFile.eof() )
    	{
    		datFile >> Val;
    		n = Val;
    		if (Val > n)
    		    n = Val;
    	}
    and it works.
    Problem is I am also wanting to get the number of rows in the file, which I got working already. But I want to get both these values in the same while not end of file block, like this:

    Code:
    while(!datFile.eof() )
    	{
    		datFile >> Val;
    		n = Val;
    		if (Val > n)
    			n = Val;
    		getline(datFile, line);
    		m++;     'note that m is the int which holds the row numbers
    	}
    but if I try this, it won't calculate the max value anymore.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The natural way to do this in C++ would be a stringstream -- a way to use >> but out of a string, rather than a file or the keyboard. If that's acceptable to you, you can probably find an example here on the board by searching for it.

  11. #11
    Sweet
    Join Date
    Aug 2002
    Location
    Tucson, Arizona
    Posts
    1,820
    You can use a stringstream to parse out the numbers per line.

    stringstream - C++ Reference

    Code:
    Read Line
    Increase Line Count
    Load string stream
    Parse int
    Check max
    Woop?

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by james890 View Post
    Ok, I've tried this:

    Code:
    	while(!datFile.eof() )
    	{
    		datFile >> Val;
    		n = Val;
    		if (Val > n)
    		    n = Val;
    	}
    do not use eof to control loop. correct forn of the loop would be
    Code:
    while(datFile >> Val)
    {
    // do something with the Val
    }
    and it works.
    Hardly... after

    Code:
    n = Val;
    the condition Val > n will be always false... so you just end with the last read number...

    Problem is I am also wanting to get the number of rows in the file, which I got working already. But I want to get both these values in the same while not end of file block, like this:

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

    Code:
    while(getline(datFile, line))
    {
        m++;
       //read integers from the line using stringstream object
    }
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Apr 2010
    Posts
    6
    I'm not sure I understand what your suggesting to do...
    Should I try to calculate both numbers in the same while loop?
    Are you telling me how to calculate 'n'?

    Sorry for my probably stupid questions.

  14. #14
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Consider this
    Code:
    string str;
    int lineCount = 0;
    getline(dataFile, str);
    lineCount++;
    stringstream oneLine(str);
    while (oneLine >> val)
    {
       ...
    }
    I believe the idea is to transform a line into a stream, with stringstream and getline (maybe there is a more direct way to do this). A stringstream (oneLine) is a stream, like a file in your case (dataFile), thus it uses the << and >> operators as you did before.

    With getline you read one by one line, so now you can count them.
    Of course, the above code is not complete. You need to check if getline succeeds and only then increase the lineCount as well as perform the checking operation.

  15. #15
    Registered User
    Join Date
    Apr 2010
    Posts
    6

    Resolved

    I've sorted it.

    Code:
    	while(datFile >> Val)
    	{	
    		if (Val > n)
    		{
    			n = Val;
    		}
    	}
    
    	datFile.clear();
    	datFile.seekg(0);
    		
    	while(getline(datFile, line))
    	{
    		m++;
    	}
    Thanks for your help.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. read numbers from a file
    By haroonie in forum C Programming
    Replies: 5
    Last Post: 03-27-2005, 11:30 PM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 09:54 AM