0

Soy try to make a code that take a .txt in imput and create dynamically a list of particle (with the structure particle created in a header file). The problem here is that when I try to printf the values of the list after it's values are implemented with the scanf function, the only thing gcc return is a list of int 0. Do you know what is wrong with my code? Thank you by advance and sorry for my bad english(I'm French)

int main(int argc, char **argv) {
FILE *p_file = NULL;
p_file = fopen(argv[1], "r");

if (p_file == NULL) {
    fprintf(stderr, "Cannot read file %s!\n", argv[1]);
    exit(EXIT_FAILURE);
}
int particle_number;

int fscanf_result = fscanf(p_file, "%d\n", &particle_number);

printf("nombre de particules : %d\n", particle_number);

double px;
double py;
double vx;
double vy;
double mass;
double radius;
double color;

int line_nbr = 0;

particle *list_of_particle;

list_of_particle = (particle*)malloc(particle_number * sizeof(particle));

fscanf_result = fscanf(p_file, "%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &py, &vx, &vy, &mass, &radius, &color);

 while (fscanf_result != EOF) {
    if (fscanf_result != 7) {
        printf("Line number %d is not syntactically correct in particles-tests!\n",
                 line_nbr);
        exit(EXIT_FAILURE);
    }

    particle *p = malloc(sizeof(particle));

    (p -> position).x = px;
    (p -> position).y = py;
    (p -> velocity).x = vx;
    (p -> velocity).y = vy;
    p -> mass = mass;
    p -> radius = radius;
    p -> color = color;

    printf("couleur du pointeur : %d\n", (p -> color));

    list_of_particle[line_nbr] = *p;
    line_nbr ++;


    printf("vérification de la couleur de la liste : %d\n", (list_of_particle[line_nbr]).color);

    printf("valeur du fscanf : %d\n", fscanf_result);

    //printf("vérification du rayon de la particule numéro %d : %lf\n", line_nbr, (list_of_particle[line_nbr] -> radius));

    fscanf_result = fscanf(p_file, "%lf,%lf,%lf,%lf,%lf,%lf,%lf\n", &px, &py, &vx, &vy, &mass, &radius, &color);

    free(p);

 }
 fclose(p_file);

p_file = NULL;

}

  • 1
    Welcome to SO. You should read and handle your compiler warnings. If you do not get a warning about mismatch of `%d` format specifier and provided `double` parameter, you need to increase warning level. A `double` needs format specifier `%f`. Also I assume you get the same output for printing `p->color` which means, your issue is not related to your array at all. – Gerhardh Feb 23 '22 at 16:34
  • Are the data in your file actually comma-separated? Do they use the comma as a decimal separator? A sample of a few lines of the input file would be useful. – Adrian Mole Feb 23 '22 at 16:35
  • @AdrianMole this is one of the rare cases, where the result of `fscanf` is actually checked. ;) – Gerhardh Feb 23 '22 at 16:37
  • You should also add a check to your loop condition to not read more than `particle_number` lines from the file. Otherwise you will write out of bounds of your allocated memory. – Gerhardh Feb 23 '22 at 16:40
  • Please remove the newline `\n` from all of the `scanf` and `fscanf` format strings and see [What is the effect of trailing white space in a scanf() format string?](https://stackoverflow.com/questions/19499060/what-is-the-effect-of-trailing-white-space-in-a-scanf-format-string) – Weather Vane Feb 23 '22 at 16:42

0 Answers0