hello,

I'm trying to import data from external file (it's a text file with fractions separated by space) and manipulate that data as a floating point. I've tried using 'fget' and 'getstring' and 'readline' and I'm getting an error that says that I'm trying to make a data out of a pointer or incompatible data type. Does anybody have any idea of how I could do this? Thanks

r.

ps. sample of external data file

1.235485 0.385924 1.256915 1.928245
0.223548 0.948562 0.658851 0.251387
2.254912 1.526842 0.125884 2.002154

Recommended Answers

All 2 Replies

If you use fgets() to read a line containing floating points you need to parse that line afterwards. sscanf() can do that for you.
fscanf() can format floating points values directly as an alternative.
Take a look at those.

If your data file contains numbers only, no need to read it in line by line manner: use fscanf directly:

double x;
int rc;
...
for (int n = 0; (rc=fscanf(f,"%lf",&x)) == 1; ++n) {
    /* you have got the next number... */
}
if (rc == EOF) {
    if (ferror(f)) {
        /* i/o error */
    } else {
        /* Normal EOF, n numbers here */
    }
} else {
    /* Bad number (bad data file) */
}
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.