0

Input :-

10.0, 15.0 ,20.0,  25.0,  30.0 , 35.0 ,40.0,  45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0 ,90.0, 95.0 ,100.0, 105.0 ,110.0, 115.0, 120.0, 125.0, 130.0
2.0  ,3.0  ,4.0  ,5.0   ,6.0  ,7.0   ,8.0,    9.0,  10.0,  11.0 ,12.0, 13.0, 14.0, 15.0,16.0, 17.0, 18.0, 19.0, 20.0,   21.0 ,  22.0 ,23.0 ,24.0, 25.0, 26.0

My output should be as below Output:-

ex

arr1= {10.0, 15.0 ,20.0,  25.0,  30.0 , 35.0 ,40.0,  45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0 ,90.0, 95.0 ,100.0, 105.0 ,110.0, 115.0, 120.0, 125.0, 130.0}

arr2 = {2.0  ,3.0  ,4.0  ,5.0   ,6.0  ,7.0   ,8.0,    9.0,  10.0,  11.0 ,12.0, 13.0, 14.0, 15.0,16.0, 17.0, 18.0, 19.0, 20.0,   21.0 ,  22.0 ,23.0 ,24.0, 25.0, 26.0}

How can I achieve this Output. What I am missing,Where I am going wrong. Please guide me.

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

int main(){

     FILE *fp = fopen("data.txt", "r");
    float *Data;
    char line[300];
    char str[300];
    char sepStrings[25][10];
    int i, j, cnt=0;

    printf("line size is %d\n",sizeof(line));
    if(fp==NULL){
        {printf ("\n Unable to open text file\n");
        exit(0);
        }
    }
    else{
        while (fgets(line, sizeof(line), fp)) {// Reading the data from file
        puts(line);
            strcpy(str,line);
            j = 0;
            cnt = 0;
            for (i = 0; i <= (strlen(str)); i++) {

                if (str[i] == ',' || str[i] == '\0') {
                    sepStrings[cnt][j] = '\0';
                    cnt++;
                    j = 0;
                }
                else{
                    sepStrings[cnt][j] = str[i];// Passing the read data from file to array
                    j++;
                }
            }

            Data = (float*)malloc(cnt * sizeof(float));
            //printing the data from array
            for (i = 0; i < cnt; i++){
                if(strlen(sepStrings[i])>0){
                   Data[i]=atoi(sepStrings[i]);
                   printf("%lf\n", Data[i]);
                                }
            }
            free(Data);
        }
    }
}
kaylum
  • 13,833
  • 2
  • 22
  • 31
user113883
  • 21
  • 3
  • What error or incorrect behaviour do you currently get? And what debugging have you done? Have you run your program in a debugger and checked things like the contents of `sepStrings` at each iteration? – kaylum Aug 04 '21 at 03:11
  • Use `strtok()` instead of processing the line character by character yourself. – Barmar Aug 04 '21 at 03:16

0 Answers0