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
#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