If you really cannot make any safe assumption about the length of the string, you should use getline (). This function takes three arguments: a pointer to a string (char **), a pointer to an int containing the size of this string and a pointer to a file, and returns the length of the string. getline () dynamically allocates space for the string (using malloc / realloc), and therefore you do not need to know the length of the string and the buffer overflow. Of course, this is not as convenient as fscanf, because you have to split the line manually.
Example:
char **line=NULL; int n=0,len; FILE *f=fopen("...","r"); if((len=getline(&line,&n,f)>0) { ... } free(line); fclose(f);
source share