#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
int main(int argc, char const *argv[])
{
char *buf = NULL;
int flen = 0;
int min = INT_MAX;
int max = INT_MIN;
int ret = 0;
FILE *fp = fopen("nums.txt", "r");
if(fp == NULL){
printf("Failed to open file\n");
exit(0);
}
fseek(fp, 0, SEEK_END);
flen = ftell(fp);
buf = malloc(flen+1);
buf[flen] = ‘\0‘;
if(buf == NULL)
{
printf("No memory\n");
fclose(fp);
exit(0);
}
fseek(fp,0L,SEEK_SET);
while(fgets(buf, flen+1, fp) != NULL)
{
if(buf[0] == ‘\n‘)
{
printf("empty line\n");
free(buf);
fclose(fp);
exit(0);
}
char *temp = strtok(buf, " ");
while(temp){
int value = atoi(temp);
min = min > value ? value : min;
max = max < value ? value : max;
temp = strtok(NULL, " ");
}
}
free(buf);
fclose(fp);
ret = max - min;
printf("ret is %d\n", ret);
return ret;
}