0

I have the following difficulties. I'm trying to read this file.

3
1 2 3
2
4 5

The numbers that are alone are the size of the array (3 and 2). And the following numbers are the array. So 3 is the size of (1,2,3) and 2 is the size of (4,5).

I wrote a code on C to read that numbers and store them in arrays that use malloc().

This is my code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
main(){
    int i=0, j, *size, *vector;

    size=(int*)malloc(sizeof(int));
    vector=(int*)malloc(sizeof(int));

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }
    else {
        do{
            fscanf(file,"%d",&size[i]);
            for(j=0;j<=size[i];j++){
                fscanf(file,"%d",&vector[j]);
            }
            i++;

        }while(feof(file)==0);
    }
    fclose(file);

return 0;
}

Is reading the file properly but if I printf the numbers I get:

3
5
2
3
4
2
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
Vladimir
  • 1
  • 1
  • [Please see this discussion on why not to cast the return value of `malloc()` and family in `C`.](http://stackoverflow.com/q/605845/2173917). – Sourav Ghosh Nov 28 '15 at 13:29

2 Answers2

1

Two things to mention here.

  1. You have allocated memory for only one variable through malloc(), but you try to access beyond the allocated memory in the do...while loop. You need to realloc(), as and when required.

  2. Please see Why is “while ( !feof (file) )” always wrong?

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
0
#include <stdio.h>
#include <stdlib.h>

int main(void){
    int i, j, size, *size_v, **vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;//scan file
    while(EOF != fscanf(file, "%d", &size)){
        for(j = 0; j < size; j++){
            fscanf(file, "%*d");
        }
        i++;
    }
    rewind(file);

    size_v = malloc(i * sizeof(int));
    vector = malloc(i * sizeof(int*));

    i = 0;//read file
    while(EOF != fscanf(file, "%d", &size_v[i])){
        vector[i] = malloc(size_v[i] * sizeof(int));

        for(j = 0; j < size_v[i]; j++){
            fscanf(file, "%d", &vector[i][j]);
        }
        i++;
    }
    fclose(file);

    //print & deallocate
    size = i;
    for(i = 0; i < size; ++i){
        printf("%d\n", size_v[i]);
        for(j = 0; j < size_v[i]; ++j){
            printf("%d ", vector[i][j]);
        }
        printf("\n");
        free(vector[i]);
    }
    free(vector);
    free(size_v);

    return 0;
}

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

int main(void){
    int i, j, size, *vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;
    while(EOF != fscanf(file, "%d", &size)){
        printf("%d\n", size);//each loop
        vector = malloc(size * sizeof(int));
        for(j = 0; j < size; j++){
            fscanf(file, "%d", &vector[j]);
        }
        for(j = 0; j < size; j++){
            printf("%d ", vector[j]);
        }
        printf("\n");
        free(vector);
        i++;
    }
    fclose(file);

    return 0;
}

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

int main(void){
    int i, j, k, size, size_sum, *size_v, *vector;

    FILE *file;
    file=fopen("file.dat", "rt");

    if (file==NULL){
        printf("Exit ...");
        exit(1);
    }

    i = 0;//scan file
    size_sum = 0;
    while(EOF != fscanf(file, "%d", &size)){
        size_sum += size;
        for(j = 0; j < size; j++){
            fscanf(file, "%*d");
        }
        i++;
    }
    rewind(file);

    size_v = malloc(i * sizeof(int));
    vector = malloc(size_sum * sizeof(int));

    i = 0;//read file
    k = 0;
    while(EOF != fscanf(file, "%d", &size_v[i])){
        for(j = 0; j < size_v[i]; j++){
            fscanf(file, "%d", &vector[k++]);
        }
        i++;
    }
    fclose(file);

    //print & deallocate
    size = i;
    size_sum = 0;
    for(i = 0; i < size; ++i){
        printf("%d\n", size_v[i]);
        for(j = 0; j < size_v[i]; ++j){
            printf("%d ", vector[j + size_sum]);
        }
        printf("\n");
        size_sum += size_v[i];
    }
    free(vector);
    free(size_v);

    return 0;
}
BLUEPIXY
  • 39,699
  • 7
  • 33
  • 70