Home > Blockchain >  How do I save floats from a txt file into a 2D array?
How do I save floats from a txt file into a 2D array?

Time:10-08

I am trying to read floats separated by "," from a txt file and save in a 2d array (31 x 4). But when I run the program the first row comes back correct, the remainder of the rows are all "0.00000".

TXT FILE

AGLHAL,AGLSOM,ANGAST1,ARWF1
0.000,0.000,0.000,71.725
0.000,0.000,xxxxx,82.778
0.000,0.000,0.000,84.265
0.000,0.000,0.000,38.964
2.258,0.000,1.628,6.646
0.000,0.000,1.245,129.554
0.000,,0.000,63.066
0.000,,0.303,84.729
0.000,0.000,-0.001,114.079
0.000,0.000,0.182,20.956
0.000,0.000,xxxxx,15.421
0.000,,ygvt,57.726
0.000,0.000,ijuhyg,85.433
0.000,,0.000,106.965
0.000,0.000,0.294,132.627
0.000,0.000,abc,46.133
12.864,0.000,0.000,1.850
42.991,aaaa,14.562,47.279
49.822,bbbb,17.713,65.436
0.000,0.000,0.000,85.881
29.429,0.000,1.322,29.684
11.784,cccc,0.000,73.187
0.000,0.000,0.000,110.376
0.000,0.000,0.000,66.343
0.886,0.000,0.588,38.150
0.000,0.000,0.000,24.694
23.437,22.697,0.000,9.468
34.642,35.602,13.136,47.170
0.000,0.000,0.000,120.737
0.000,0.000,0.000,196.755
0.000,0.000,0.000,176.882

CODE:

#define _CRT_SECURE_NO_WARNINGS 1
#define ROWS 32
#define COLUMNS 8
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <string.h> 

void openFile(char* str) {

    FILE* fp;
    int x = 1;
    char s[50] = { 0 };

    while (x == 1) {

        printf("Type the filename to process >\n");
        (void)scanf("3s", s);

        if ((fp = fopen(s, "r")) == NULL)
        {
            printf("error: file not found\n\n");
            x = 1;
        }
        else x = 0;

        strcpy(str, s);
    }
}

int main()
{
    FILE* fp;
    float data[ROWS][COLUMNS] = { 0 };
    int length = 0;
    int x = 1;
    char DUID[100];
    float max, sum, average = 0;
    int i, averagesAboveZero, averagesAboveAverage, rows;
    char filename[124];

    openFile(filename);

    fp = fopen(filename, "r");
    (void)fscanf(fp, "%s,", DUID);


    for (rows = 0; rows < 32; rows  )
        if (fscanf(fp, "%f,%f,%f,%f", &data[rows][0], &data[rows][1], &data[rows][2], &data[rows][3]) == EOF)
            break;

    fclose(fp);

    for (int row = 0; row < rows; row  )
        printf("This is row %d = %f, %f, %f, %f\n", row, data[row][0], data[row][1], data[row][2], data[row][3]);

    fclose(fp);
}

OUTPUT

Type the filename to process >
2018-January-Averages.txt
This is row 0 = 0.000000, 0.000000, 0.000000, 71.724998
This is row 1 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 2 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 3 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 4 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 6 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 7 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 8 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 9 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 10 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 11 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 12 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 13 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 14 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 15 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 16 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 17 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 18 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 19 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 20 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 21 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 22 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 23 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 24 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 25 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 26 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 27 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 28 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 29 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 30 = 0.000000, 0.000000, 0.000000, 0.000000
This is row 31 = 0.000000, 0.000000, 0.000000, 0.000000

CodePudding user response:

if ((fp = fopen(s, "r")) == NULL) opens the file and leaves it open, then you try to open it again at fp = fopen(filename, "r");. Which fails and if you checked the result of fopen here too like you did previous, you would have found that problem.

Then later you call fclose(fp); twice for some strange reason.

Also as someone pointed out in comments, the scanf("3s", s); doesn't make sense. If you want to read at most 123 characters consider using fgets instead.

There might be other problems as well.

CodePudding user response:

Check the below source. I hope it will be helpful. I use "strok" function to split the string by ','; scanf doesn't support splited input by some character, but Number.

The test data include strings not only float numbers, so you should check each item whether it is a number or string.

int main()                                                                                          
{                                                                                                                                                                                                      
  FILE* fp;                                                                                         
  float data[ROWS 1][COLUMNS] = { 0, };                                                             
  int length = 0;                                                                                   
  int x = 1;                                                                                        
  char DUID[100];                                                                                   
  float max, sum, average = 0;                                                                      
  int i = 0, averagesAboveZero, averagesAboveAverage, rows;                                         
  char filename[124] = "test.txt";                                                                  
  char *p;                                                                                          
                                                                                                    
  openFile(filename);                                                                            
                                                                                                    
  fp = fopen(filename, "r");                                                                        
  (void)fscanf(fp, "%s", DUID);                                                                                                                       
                                                                                                    
  for (rows = 0; rows < 32; rows  ) {                                                               
    if(feof(fp)) break;                                                                             
    fscanf(fp, "%s", DUID);                                                                         
    i = 0;                                                                                          
    p = strtok(DUID, ",");                                                                          
    do {                                                                                            
      if(sscanf(p, "%f", &data[rows][i]) != 1) data[rows][i] = 0;                                   
      p = strtok(NULL, ",");                                                                        
      i  ;                                                                                          
    } while(p);                                                                                     
  }                                                                                                 
                                                                                                    
  fclose(fp);                                                                                       
                                                                                                    
  for (int row = 0; row < rows; row  )                                                              
    printf("This is row %d = %f, %f, %f, %f\n", row, data[row][0], data[row][1], data[row][2], data[
row][3]);                                                                                           
}                                                                                                   

  •  Tags:  
  • c
  • Related