Help with find highest and smallest number in a file with c

Input file:

#data_1
AGDG
#data_2
ADG
#data_3
ASDDG
DG
#data_4
A

Desired result:

Highest 7
Slowest 1

code that I try but failed to archive my goal :frowning:

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAXLINES        32768


int lines[MAXLINES];
int line_count = 0;

int qsort_cmp(const void *p1, const void *p2)
{
        return (*(int *)p2 - *(int *)p1);
}

int main(int argc, char**argv)
{
        FILE *fh;
        char buf[4096];

        int l, largestNum, smallestNum;

        if (argc != 2)
        {
                printf("Usage: %s <input file>\n", argv[0]);
                return -1;
        }

        if ((fh = fopen(argv[1], "r")) == NULL)
        {
                perror("fopen");
                return -1;
        }

        while (fgets(buf, sizeof(buf), fh))
        {
                int len = strlen(buf);
                /* strip newline at end */
                buf[--len] = 0;
                /* new content block */
                if (buf[0] == '#') {
                        lines[++line_count] = 0;
                        continue;
                }
                /* keep record */
                lines[line_count] += len;
        }
        largestNum = lines[0];
        smallestNum = lines[0];
        for (l = 1; l < line_count; l++)
{

        if (lines[l] > largestNum)
        {
        largestNum = lines[l];
    printf("Highest %d\n", largestNum);
        }
        else if (lines[l] < smallestNum)
        {
        smallestNum = lines[l];
    printf("Smallest %d\n", smallestNum);
return 0;
        }

        }
}

When reading the input file, calculate the length of each "#".
From the above input file, the length is 4,3,7,1.
Thus, highest number is 7 and smallest is 1.
Many thanks for any advice :slight_smile:

Try this.

#include <stdio.h>
#include <string.h>
#include <stdlib.h>


int largest = 0;
int smallest = 4096;


int main(int argc, char**argv)
{
    FILE *fh;
    char buf[4096];
    int l, largestNum, smallestNum;
    int len = 0;

    if (argc != 2)
    {
        printf("Usage: %s <input file>\n", argv[0]);
        return -1;
    }

    if ((fh = fopen(argv[1], "r")) == NULL)
    {
        perror("fopen");
        return -1;
    }

    while (fgets(buf, sizeof(buf), fh))
    {
        if (buf[0] != '#') {
            len += (strlen(buf) - 1);
        } else if (len > 0) {
            if ( len > largest) largest = len;
            if ( len < smallest) smallest = len;
            len = 0;
        }
    }

    if (len > 0 ) {
       if (len > largest) largest = len;
       if (len < smallest) smallest = len;
    }

    printf("Highest %d\n", largest);
    printf("Smallest %d\n", smallest);

    return 0;
}

There are more elegant ways of doing this but I have kept it simple and omitted boundary checks and suchlike so that you should be able to understand the logic.

1 Like

Thanks, fpmurphy
Your program work fine :slight_smile:
I just a bit confusing about the following code, need your advice:

len += (strlen(buf) -1);

Can I know what is the logic and explanation that you decide to set it as "-1"?
Will your program give difference length calculation if the length content "\n" or "\r"?
eg.

#data_3
ASDDG
DG
#data_3
ASDDGDG

Many thanks again :slight_smile: