-5

I have to store each data in an array. How do I read these data from a file?

120 5.0000000000000000E-01   -5.0000000000000000E-01  5.0000000000000000E-01  
5.0000000000000000E-01   -5.0000000000000000E-01  -5.0000000000000000E-01 
5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01  
5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01 
-5.0000000000000000E-01

The data is mixture of integers, floating points and exponentials. The spaces between consecutive data are not constant, hence I cannot use a straightforward fscanf(). I also have to change them to integer hence, I cannot find alternative to fscanf() as I can specify the type specifier as %e in fscanf() argument and later change them to integer. I have tried fgetc() as well. Please show me a way.

edit.

To use fscanf() I need to have constant number of spaces or commas or anything between consecutive data. Here, the number of spaces between each data are not constant. So, I need to also implement a space checker in between.That's why I used fgetc() at a part.

#include<stdio.h>

int main()
{
int i=0,c,a[13];
FILE *fp;
fp=fopen("test.txt","r");
if(fp==NULL)
{
    printf("Error");
}
else
{
    i=0;
    while(1)
    {
        c=fgetc(fp);
        //printf("\nc = %c",c);
        if(feof(fp))
        {
            break;
        }
        else if(c!=" ")
        {
            fscanf(fp,"%d",&a[i]);
            printf("%d\n",a[i]);
            i++;
        }
    }

}
fclose(fp);
return 0;
}

The data file is 2 m.b. I have just posted a part of it. Other parts have floating points, which are not exponentials like here.

  • 3
    `Please give me a code.`...have a downvote instead. – Sourav Ghosh Jun 15 '16 at 12:05
  • 3
    Welcome to Stack Overflow! Please show your research effort till time. Please read [Ask] page first. – Sourav Ghosh Jun 15 '16 at 12:05
  • 3
    Please offer me lots of money then I'll give you lots of code. – Bathsheba Jun 15 '16 at 12:06
  • 3
    Why can't you use “straightforward `fscanf()`?” Note that the `e` formatting directive does the right thing for all styles of floating point numbers. – fuz Jun 15 '16 at 12:09
  • There seems to be only one single integer in the input you show, and all the floating-point numbers have the same format. So I really don't see what the problem is. Please tell us what you have tried, edit your question and copy-paste the relevant code into the question body, elaborate on what works ad doesn't work with your show code. – Some programmer dude Jun 15 '16 at 12:14
  • The input file is *always* one integer followed by 13 floating point values? Then you don't need any checking, just read the integer, and then in a `for` loop read the floating point values. All using e.g. `fscanf`. – Some programmer dude Jun 15 '16 at 12:19
  • You can also read the whole file into a double array and then copy it to an int array. `fscanf` with `%le` will skip all whitespaces. – mch Jun 15 '16 at 12:20

2 Answers2

0

This program reads from the file and converts the tokens to floats. You should read an entire line and tokenize it. Then you just need to create an array and add to the array at the right place in the code.

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

int main(void) {
    FILE *sp1;
    char line[256];
    double array[256];
    int i = 0;

    sp1 = fopen("data.txt", "r");
    while (1) {
        if (fgets(line, 150, sp1) == NULL) break;
        char *p = strtok(line, " ");
        while (p != NULL) {
            double fd = atof(p);
            array[i++] = fd;
            p = strtok(NULL, " ");
        }
    }

    for(int j=0;j<i;j++)
        printf("%f\n", array[j]);

    return 0;
}

data.txt

120 5.0000000000000000E-01   -5.0000000000000000E-01  5.0000000000000000E-01
5.0000000000000000E-01   -5.0000000000000000E-01  -5.0000000000000000E-01
5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01
5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01
-5.0000000000000000E-01

Output

120.000000
0.500000
-0.500000
0.500000
0.500000
-0.500000
-0.500000
0.500000
-0.500000
0.166667
0.500000
-0.500000
-0.166667
-0.500000
Niklas Rosencrantz
  • 25,640
  • 75
  • 229
  • 424
0

Try the below code, it will read all the numbers from input file as doubles.

White spaces between numbers do not matter.

#include<stdio.h>
#include<stdlib.h>

int main(){


    FILE * fp;
    double num;

    fp = fopen("input.txt", "r");

    if (fp == NULL){
        printf("Unable to open file, terminating ...");
        exit(1);
    }
    else {
        while( fscanf(fp, "%lf", &num) == 1){
            printf("%lf %e\n", num, num);
        }
        fclose(fp);
    }
    return 0;
}

input.txt: (added a few more white-spaces and numbers in yours)

120 5.0000000000000000E-01     -5.0000000000000000E-01  5.0000000000000000E-01

5.0000000000000000E-01  -5.0000000000000000E-01                 -5.0000000000000000E-01

5.0000000000000000E-01   -5.0000000000000000E-01  1.6666666666999999E-01
5.0000000000000000E-01   -5.0000000000000000E-01  -1.6666666666999999E-01   17.3 16.55511


-5.0000000000000000E-01

Output:

120.000000 1.200000e+02
0.500000 5.000000e-01
-0.500000 -5.000000e-01
0.500000 5.000000e-01
0.500000 5.000000e-01
-0.500000 -5.000000e-01
-0.500000 -5.000000e-01
0.500000 5.000000e-01
-0.500000 -5.000000e-01
0.166667 1.666667e-01
0.500000 5.000000e-01
-0.500000 -5.000000e-01
-0.166667 -1.666667e-01
17.300000 1.730000e+01
16.555110 1.655511e+01
-0.500000 -5.000000e-01
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87