-1

I have a txt file which has the following data:

5
676.54
6453.22
7576.776
8732.2
6

I need to read the file in a C program line by line. But whatever method I use to read the file, I get the value as a string. I need to get the values as int and float types(depending on the values). Is there any way I can do this? Operating platform is linux.

Harikrishnan
  • 7,765
  • 13
  • 62
  • 113

4 Answers4

2

Use fscanf()

FILE* f = fopen(name, mode);
float d;
fscanf(f, "%f\n", &d);

But this will always give you floats (use %lf for doubles).

If you want to know what's the format read a line as a string and then use strtod() to try to read it as a int first and see how much of the string was used (if all, it's an int) and strtod() to read as a double.

Caladan
  • 1,471
  • 11
  • 13
  • "In the absence of a size modifier, the application shall ensure that the corresponding argument is a pointer to **float**." -- http://pubs.opengroup.org/onlinepubs/009695399/functions/fscanf.html – autistic Apr 10 '13 at 14:58
  • `"%f"` is the (or at least "a") correct format for a `double`. There is no format for a `float`, because if you attempt to pass a `float` to `printf`, it'll be promoted to `double` before `printf` receives it http://stackoverflow.com/questions/4264127/correct-format-specifier-for-double-in-printf/4264154#4264154 – MOHAMED Apr 10 '13 at 15:39
1

If you want to read float numbers use fscanf with %f percent code

float var;

while (fscanf(yourfilepointer, "%f", &var) == 1)
{
    // do your stuff with your float var
}
Davide Berra
  • 6,387
  • 2
  • 29
  • 50
0

read the file line by line as a float X

If X == ((int) X) then it's an int

Else it's a float

float X;
int a;
while(fscanf(file, "%lf", X)>0) {

   if(X == ((int) X)) {
      a = ((int) X);
      printf("scanned value is integer: %d\n", a);
   } else {
      printf("scanned value is float: %lf\n", X);

   }

}
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
0
#include <stdio.h>
#include <stdlib.h>

typedef union _num {
    long int integer;
    double floating;
} NumberU;

typedef struct _n {
    char type;
    NumberU data;
} Number;

int main() {
    FILE *fp;
    int c,i;
    char buff[16];
    Number num[10];

    fp=fopen("data.txt","r");
    for(c=0;c<10 && NULL!=fgets(buff,sizeof(buff),fp);){
        char *ck;
        long di;
        double dd;
        di=strtol(buff, &ck, 0);
        if(*ck == '\n'|| *ck == '\0'){
            num[c].type = 'i';
            num[c++].data.integer = di;
            continue;
        }
        dd=strtod(buff, &ck);
        if(*ck == '\n'|| *ck == '\0'){
            num[c].type = 'f';
            num[c++].data.floating = dd;
            continue;
        }
        fprintf(stderr, "error input:%s\n", buff);
    }
    fclose(fp);

    for(i=0;i<c;++i){
        if(num[i].type=='i')
            printf("%ld\n", num[i].data.integer);
        else if(num[i].type=='f')
            printf("%lf\n", num[i].data.floating);
    }

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70