#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
char *inputString(FILE* fp, size_t size){
//The size is extended by the input with the value of the provisional
char *str;
int ch;
size_t len = 0;
str = realloc(NULL, sizeof(char)*size);//size is start size
if(!str)return str;
while(EOF!=(ch=fgetc(fp)) && isspace(ch));//skip space chars
ungetc(ch, fp);
while(EOF!=(ch=fgetc(fp)) && !isspace(ch)){
str[len++]=ch;
if(len==size){
str = realloc(str, sizeof(char)*(size+=16));
if(!str)return str;
}
}
str[len++]='\0';
return realloc(str, sizeof(char)*len);
}
enum {false, true };
int readInt(FILE *fp, int *n){
char *token, *endp;
long wk;
for(;;){
token = inputString(fp, 16);//separated by space character
if(!*token){//EOF
free(token);
return false;
}
wk = strtol(token, &endp, 0);
if(*endp=='\0')break;//success read int
free(token);
}
*n = (int)wk;
free(token);
return true;
}
int main(void){
FILE *file;
int n, m, max,i;
n = m = max = 0;
file = fopen("filename.txt", "r");
readInt(file, &n);
readInt(file, &m);
readInt(file, &max);
/*
printf("debug:");
if(readInt(file, &i)==false){
printf("false\n");
}
*/
fclose(file);
printf("m = %d\n", m);
printf("n = %d\n", n);
printf("max = %d\n", max);
return 0;
}