addictedtohaskell
addictedtohaskell

Reputation: 541

fscanf() not reading and crashing

I'm currently trying to parse a .csv file and extract the fields to some dinamically allocated arrays in C. I tried parsing the file by:

  1. Counting the characters in the file
  2. Allocating a char * big enough to hold all the chars
  3. Tokenize the input using strtok so it can be stored in the arrays

However, this approach was not successfull, because the .csv contains 10^10 characters and my computer has low memory (below 2 GB).

But, as the file containg only 10^5 lines, I tried another approach: I opened the .csv file and read it token by token, removing the commas (,) and placing whitespaces where needed. After that, I got a new text file with 4 fields per line:

Integer  Double      Double      Double
  Id    Latitude    Longitude    Weight

I'm currently trying to read line by line from that file using fscanf, and then storing the values I read into 4 arrays allocated using malloc. The code is here:

int main()
{
   const int m = 100000;
   FILE * gift_file = fopen("archivo.txt", "r");
   if( gift_file != NULL) fprintf(stdout, "File opened!\n");

   create_saving_list(m , gift_file);


   return 0;
}

void create_saving_list( int m, FILE * in )
{
    unsigned int index = 0;
    double * latitude = (double *)malloc(m*sizeof(double));
    if( latitude == NULL ) fprintf(stdout, "Not enoug memory - Latitude");

    double * longitude = (double *)malloc(m*sizeof(double));
    if( longitude == NULL ) fprintf(stdout, "Not enoug memory - Longitude");

    double * weight = (double *)malloc(m*sizeof(double));
    if( weight == NULL ) fprintf(stdout, "Not enoug memory - Weight");

    int * id = (int *)malloc(m*sizeof(int));
    if( id == NULL ) fprintf(stdout, "Not enough memory - ID");

    while( fscanf( in, "%d %lf %lf %lf\n", id[index], latitude[index], longitude[index], weight[index] ) > 1 )
    {
        index += 1;
    }

    /* Processing of the vector ...*/


}

I have been able to track the memory allocations and verify they execute correctly. The trouble is inside the while(), as fscanf() call seems correct to me, but it causes a crash inmediately. I tried printing the index to see it it is changed, but it is not printed.

Any kind of help is welcomed.

Upvotes: 0

Views: 329

Answers (3)

Oo.oO
Oo.oO

Reputation: 13415

I think you should provide address of element in here:

fscanf( in, "%d %lf %lf %lf\n", &id[index], &latitude[index], &longitude[index], &weight[index])

It should work

Upvotes: 2

Ed Heal
Ed Heal

Reputation: 60037

You need pointers to ints/floats in the fscanf i.e.

fscanf( in, "%d %lf %lf %lf\n", &id[index], &latitude[index], &longitude[index], &weight[index] ) == 4 )

Also check that it is equal to 4 as you want all the format to be used and all the variables to be given a value

Upvotes: 3

Jay
Jay

Reputation: 24905

fscanf( in, "%d %lf %lf %lf\n", id[index], latitude[index], longitude[index], weight[index] ) 

should be

  fscanf( in, "%d %lf %lf %lf\n", &id[index], &latitude[index], &longitude[index], &weight[index] ) 

You need to pass the address of the variables to fscanf

Upvotes: 1

Related Questions