Numbers can be complicated. For example, whether you accept scientific notation as floating point numbers can cause your program have a double number of lines.
Of course, the simplest way to grab any text is using the regular expressions. If you are using POSIX-capable systems, the natural way is to use the POSIX regex library. As shown in this program: regex-example.c
The POSIX regex functions are declared in regex.h and the major functions are regcomp() to compile the regex, regexec() to perform a match, regerror() to collect error messages and regfree() to reclaim the memory occupied by regcomp().
However, if you just need simplest number-reading function in C, probably stdtod() is good enough to use. For example, the following code excerpt:
char *ptr, *ptrNext, buff[BUFSIZE];
FILE* fpMatrix=fopen("matrix.txt","r");
....
for(i=0;i<m;i++) {
if(!fgets(buff, BUFSIZE, fpMatrix)) {
perror("Failed to finish reading the whole matrix");
exit(1);
};
ptr = buff; /* We start at the beginning of *buf */
for(j=0;j<m;j++) {
R[i][j]=strtod(ptr, &ptrNext);
if(ptr==ptrNext) {
perror("Failed to finish reading a row of matrix");
exit(1);
};
ptr = ptrNext; /* Shift to next number */
};
};
The above excerpt is to read in a m-by-m matrix with each element is a double-precision float. The file is presented in rows as lines and each element in a row separated by some word-separating characters like spaces or tab. The validity of the above excerpt is due to the use of second parameter to strtod() function.