• 0
Votes
name

A PHP Error was encountered

Severity: Notice

Message: Undefined index: userid

Filename: views/question.php

Line Number: 191

Backtrace:

File: /var/www/html/cnasolution/application/views/question.php
Line: 191
Function: _error_handler

File: /var/www/html/cnasolution/application/controllers/Questions.php
Line: 419
Function: view

File: /var/www/html/cnasolution/index.php
Line: 315
Function: require_once

name Punditsdkoslkdosdkoskdo

reading columns from a txt file

i have a txt file that has 5 columns like in the image below, and i want to store each one of those values, like for example to have an array that contains the values in the first column, another array for the values of the second column and so on. i managed to read the first column using the following code, but i'm not sure how to read the others. Thanks in advance.

int main() { char *value=NULL; double Training[401], Test[50]; int i=0, j=0; double newvalue; double line; double *a=NULL; double val1[10], val2[10], val3[10], val4[10]; size_t line_buf_size = 0;  FILE *in_file  = fopen("turbine.txt", "r");         if (in_file == NULL)         {           printf("Error! Could not open file
");           exit(-1);         }          while(i<4) // reading the first 4 values of the first column , works well          {             getline(&value, &line_buf_size, in_file);             fscanf(in_file,"%s%*[^
]",value);             sscanf(value, "%lf", &newvalue);             Training[i]=newvalue;             i++;         }          while (j<4) // this is my attempt to read 4 columns, didn't want to run the code for the whole file so i'm just testing in it on 4 lines, no results and no error             {                     //line=getline(&a, &line_buf_size, in_file);                     //fscanf(in_file, "%s", a);                     scanf (in_file, "%lf %lf %lf %lf %lf ", &val1[0], &val2[1], &val3[2], &val4[3]);                     printf("The values are: %lf %lf %lf %lf
", val1, val2, val3, val4);                 j++;             }  fclose (in_file); return 0;    } 

txt file image

Download script demo [LINK]
Download script demo [LINK 2] Download script demo [LINK 2]

I think you should be able to do it like this:

char line[100]; int nLines = 0; while (fgets(line, 100, in_file)) {     sscanf(line, "%lf%lf%lf%lf%lf", &val1[nLines], &val2[nLines], &val3[nLines], &val4[nLines], &val5[nLines]);     nLines++; }  for (int i = 0; i < nLines; i++) {     printf("%lf %lf %lf %lf %lf
",  val1[i], val2[i], val3[i], val4[i], val5[i]); } 

I tested this on my own version of test file:

1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 11.0 9.0 11.0 23.04 34.1 34.1 45.0 

when I ran this program with this file, I was able to see desired input:

c-posts : $ gvim readingColumns.c  c-posts : $ cat turbine.txt  1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 11.0 9.0 11.0 23.04 34.1 34.1 45.0 c-posts : $ gcc readingColumns.c  c-posts : $ ./a.out  1.000000 2.000000 3.000000 4.000000 5.000000 6.000000 7.000000 8.000000 11.000000 9.000000 11.000000 23.040000 34.100000 34.100000 45.000000 
see demo
  • 0
Reply Report