I am trying to read a csv file and store it into a 2D array, However it give me "segmentation fault" each time I run it.
Here is my code

#include <stdlib.h>

int main(void)
{
   const char filename[] = "a.txt";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char *line;
      int i = 0, j=0, read=0;
      double array [2][10];
      line= malloc (80 * sizeof (char));

      while ( i < 2 )
      {
         if ( fgets(line, sizeof line, file) == NULL )
         {
            break;
         }
         for (j=0;j <20 ; j++){ 
          sscanf(line, "%lf,",&array[i][j],&read );
	 //printf("%d ",read);
          line+=read;
         }
            ++i;
         
      }
      fclose(file);

   else
   {
      perror(filename);
   }
   return 0;
}

Here is my a.txt

301.0,36,0.285,2.88,15.000,301.0,36,0.285,2.88,15.000,301.0,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000
302,88,0.247,2.88,75.500,301,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000,301,36,0.285,2.88,15.000

Moreover how can change the first 'While' loop to make it more sufficient, so instead of '2' it should predicet number of lines !

Thank you in advance.

Recommended Answers

All 3 Replies

> if ( fgets(line, sizeof line, file) == NULL )
line is a pointer, so sizeof is going to give you 4 (the size of the pointer), not 80 (the amount of memory it points at)

> line+=read;
Make a copy of the pointer before you start modifying it.

> sscanf(line, "%lf,",&array[j],&read );
Try "%lf,%n" as the format, to update read with the number of characters processed.

> so instead of '2' it should predicet number of lines !
You can't predict the number of lines.
Normally, I do

char buff[BUFSIZ];
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
  // stuff
}

then allocate what I need as the true extent of the file becomes apparent.

> if ( fgets(line, sizeof line, file) == NULL )
line is a pointer, so sizeof is going to give you 4 (the size of the pointer), not 80 (the amount of memory it points at)

> line+=read;
Make a copy of the pointer before you start modifying it.

> sscanf(line, "%lf,",&array[j],&read );
Try "%lf,%n" as the format, to update read with the number of characters processed.

> so instead of '2' it should predicet number of lines !
You can't predict the number of lines.
Normally, I do
char buff[BUFSIZ];
while ( fgets( buff, BUFSIZ, fp ) != NULL ) {
// stuff
}

then allocate what I need as the true extent of the file becomes apparent.

This is my code now and still giving the same error

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


int main(void)
{
   const char filename[] = "a.txt";
   FILE *file = fopen(filename, "r");
   if ( file != NULL )
   {
      char *ptr;
      int i = 0, j=0, read=0;
      double array [2][10];
      char line[80];

      while ( fgets(line, sizeof line , file) != NULL  )
      {
         ptr=line;
         for (j=0;j <20 ; j++){
          sscanf(ptr, "%lf,%n",&array[i][j],&read );
          ptr+=read;
         }
            ++i;

      }
      fclose(file);

   }
   else
   {
      perror(filename);
   }
   return 0;
}

Please help i am stuck at this point

> double array [2][10];
You read 20 later on
for (j=0;j <20 ; j++)

Also, limit your initial loop to how many rows in your array. while (i < 2 && fgets(line, sizeof line, file) != NULL) {

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.