Problem with getting largest number read from a file?

This is a discussion on Problem with getting largest number read from a file? within the C Programming forums, part of the General Programming Boards category; Hello. I have a program that I have to write that reads 20 ints in from a file and then ...

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    28

    Problem with getting largest number read from a file?

    Hello. I have a program that I have to write that reads 20 ints in from a file and then lists the greatest and smallest ones. For right now I'm just trying to make it run to get the largest number, and then I'll work on the smallest. The program compiled, but when I ran it it gave me some really LARGE number, and I'm not sure why. I used a past model we did to find the largest of a list of test grades in an array, and tried to apply it here, but I'm thinking that may not be the best way to go about this? This is what I have:

    Code:
    # include <stdio.h>
    
    int main()
    {
            char filename[50];
            int numbers[20];
    
            printf ("Please enter the file that you wish to open: ");
            scanf("%s", filename);
    
            FILE *infile=fopen(filename, "r");
            if(infile==NULL)
            {
                    printf( "The file was not successfully opened");
            }
    
            fscanf(infile, "%d", numbers);
    
            int m = numbers[0];
            int i;
            for(i=0; i < sizeof(numbers)/(*numbers); i++)
            {
                    if(numbers[i]>m)
                    {m = numbers[i];}
            }
    
            printf("The largest number is %d", m);
            fclose(infile);
    
            return 0;
    }
    Thank you for any help!

  2. #2
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    I am not sure what is the format of the file that you are reading. I think this line "fscanf(infile, "%d", numbers);" will read one number and put it on numbers[0]. Because when you pass an array to a function you are actually passing a pointer to the first item on the array.

    And since the rest of the array is uninitialized you will get the highest junk value on the array.

    That's just my theory I haven't tested your code.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    It's a .txt file and it looks exactly like this:

    Code:
    23 11 22 98 55 35 43 8 60 31 59 27 95 19 2 81 94 5 47 20
    Hmm, would I not use an array maybe? But, I also can't see declaring 20 different variables, either?

    Thank you for your help!

  4. #4
    Registered User
    Join Date
    Sep 2008
    Location
    Toronto, Canada
    Posts
    1,831
    You're just reading one number then? fscanf should be inside some loop.

  5. #5
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    nonoob is right. put the fscanf inside the loop.

    You don't need to use an array. Just store it in a variable then compare it to 'm'. Here is a sample code:

    Code:
    int number;
    for(i=0; i < 20; i++) {
       fscanf(infile, "%d", number);
       if(number>m)    {m = number;} }

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    Oh, I can do that? This is what it says in my notes:

    What NOT to do with File IO

    · Read ALL patterned data in ONE pass in a loop
    o used a while loop
    o but read ALL data in ONE iteration of the loop, so didn’t use the loop

    · printf or scanf
    o YOUR NOT GETTING ANYTHING FOR THE KEYBOARD!!
    o I am NOT to see a scanf UNLESS
    § getting the file FROM THE USER
    o printf DOES NOT DISPLAY AN ENTIRE FILE
    § you need to read in the line from the file first and place into a string variable
    § then display THE VARIABLE

    · Use a for loop to read the values inside the file.
    o You might know how many values are there with the files I GIVE you
    § but what if you didn’t know how many where going to be there, but you knew the pattern?

    No No #3
    Using a for loop to read data
    Code:
    for (int i = 0; i < 9; i++)
    {
    fscanf(infile, "%d", &num);
    total += num;
    }
    // how many values will this read?

    Would this be considered a different scenario, though? That's the only reason I didn't try it.

    Thanks!

  7. #7
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    That's the beauty of programming. Your mind is the limit on how you can solve a problem. I can't fully understand your notes but this is how I would write my code.

    Code:
    int m = 0, ctr = 0;
        while((ctr = fscanf(infile, "%d", &number)) > 0) {
            if(number > m)
                m = number;
        }
    You don't need to know how many numbers on the file since if fscanf fails to read a number it will return a value < 0. You also don't need an array since you only need to check the value once.

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    That's the beauty of programming. Your mind is the limit on how you can solve a problem. I can't fully understand your notes but this is how I would write my code.

    Code:
    int m = 0, ctr = 0;
        while((ctr = fscanf(infile, "%d", &number)) > 0) {
            if(number > m)
                m = number;
        }
    You don't need to know how many numbers on the file since if fscanf fails to read a number it will return a value < 0. You also don't need an array since you only need to check the value once.
    That line does not do what you think it does... your code is actually testing whether the inner bracket (ctr = fscanf(infile, "%d", &number)) is true... whether ctr was successfully assigned to by the return value of fscanf(), you are not testing ctr or the return value of scanf() itself...

    Try it like this...
    Code:
    while (fscanf(infile, "%d", &number) > 0)
    Now you are comparing the return value of scanf() to 0 and you don't need the ctr variable at all.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    This line (ctr = fscanf(infile, "%d", &number)) will assign the result of fscanf to ctr then ctr will be evaluated with '> 0'. But your code is better and more simple thanks.

  10. #10
    Registered User
    Join Date
    Sep 2011
    Posts
    28
    I got it!!! Thank you guys sooo much!

    Here is the finished code. I also went ahead and did the smallest number as well.
    Code:
    # include <stdio.h>
    
    
    int main()
    {
            char filename[50];
            int numbers;
    
    
            printf ("Please enter the file that you wish to open: ");
            scanf("%s", filename);
    
    
            FILE *infile=fopen(filename, "r");
            if(infile==NULL)
            {
                    printf( "The file was not successfully opened");
            }
    
    
            int m = 0;
            int n= 100;
            while (fscanf(infile, "%d", &numbers) > 0)
            {
                    if(numbers > m)
                    m = numbers;
    
    
                    if(numbers < n)
                    n = numbers;
            }
    
    
            printf("The largest number is %d\n", m);
            printf("The smallest number is %d\n", n);
            fclose(infile);
    
    
            return 0;
    }
    Thank you again!

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by mnd22 View Post
    This line (ctr = fscanf(infile, "%d", &number)) will assign the result of fscanf to ctr then ctr will be evaluated with '> 0'.
    No, it won't... the result of the assignment is evaluated outside the braces, not the result of scanf(). The bracketed part is comlpleted, ctr is assigned, the result is TRUE and it will always be TRUE.

    But your code is better and more simple thanks.

  12. #12
    Registered User
    Join Date
    Nov 2011
    Posts
    52
    I tested ((ctr = fscanf(infile, "%d %d", &number)) > 0). It puts 2 to ctr then exited as expected when finished reading the file. As I understand that's why the assignment is inside the bracket so it will be evaluated first. I am still new to C now I am confused but the code that I provided works on my machine.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Largest Number
    By lukesowersby in forum C Programming
    Replies: 3
    Last Post: 03-26-2009, 05:53 AM
  2. largest number prime number that can be produced...
    By ElemenT.usha in forum C Programming
    Replies: 8
    Last Post: 02-17-2008, 12:44 AM
  3. Find largest and second largest number (help)
    By Arkon in forum C++ Programming
    Replies: 6
    Last Post: 01-20-2006, 10:21 PM
  4. Max number of bytes for a read file
    By NewGuy100 in forum C Programming
    Replies: 2
    Last Post: 08-18-2005, 06:37 AM
  5. Determining the Largest number??
    By gqchynaboy in forum C++ Programming
    Replies: 4
    Last Post: 08-28-2003, 11:27 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21