-1

So I am new to C coding and am struggling to understand what I am doing wrong. I keep getting the error mentioned in the title at the lines I have left comments on below.

typedef struct input{
    double x;
    double y;
    double u;
    double v;
    double rho;
} input_t; 

void read_file();

void read_file(){
    int i = 0, count = 0;
    char c;

    FILE *fp;

    fp = fopen ("flow_data.csv", "r");
    if (fp == NULL){
        printf("Error reading file, exiting...");
        exit(EXIT_FAILURE);
    }
    while (!feof(fp)) {
        fscanf(fp, "%f %f %f %f %f", &input_t[i]->x, &input_t[i]->y, &input_t[i]->u, &input_t[i]->v, 
        &input_t[i]->rho); //getting the issue here
            i++;
    }
    for (i = 1; ; i++) {
        printf("x: %f, y: %f, u: %f, v: %f, rho: %f \n", &input_t[i]->x, &input_t[i]->y, 
        &input_t[i]->u, &input_t[i]->v, &input_t[i]->rho); //getting the issue here
    }
    for (c = getc(fp); c != EOF; c = getc(fp)) {
        if (c == '\n') {
            count = count + 1;
        }
    }
    printf("The file has %d lines\n ", count);
}

I've tried looking up solutions online but am struggling to see what I am doing wrong. Any tips or guidance would be great :) Thanks in advance!

  • You seem to have some misunderstanding about the basics. You're using `input_t` (which is a type name) as an array. You wouldn't write `fscanf(..., &int[i]);`, would you? – HolyBlackCat Aug 23 '20 at 10:52
  • What is the input file format? Your `scanf()` won't go beyond `,`. – MikeCAT Aug 23 '20 at 10:58

1 Answers1

2
  • input_t is a type name and it cannot be used as array. You will have to declare an array variable like input_t data[100000]; and use it like data[i] instead of input_t[i].
  • -> operator is for accessing members of structures that is pointed at by a pointer. You should use . operator to access structures directly.
  • In scanf(), %f is for reading float and you have to use %lf (add l) to read double. (It is good to use %f for printing double via printf())
  • Your usage of while (!feof(fp)) is wrong. You should check if fscanf() is successful before doing i++;.
  • The loop for (i = 1; ; i++) will run infinitely and it will lead to accessing out-of-range. You should use another variable for this loop and limit this loop upto i (the value of i before entering this loop).
  • You passed double* for %f in printf() while double is expected. This will invoke undefined behavior. You should remove all &s in the arguments for printf().
  • The type of the variable c should be int instead of char because getc() returns int and assigning that to char will make hard to distinguish between normal characters and EOF.
  • You should do fclose(fp); after successful open of the file and after reading what is needed.

Fixed code:

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

#define MAX_DATA_NUM 100000

typedef struct input{
    double x;
    double y;
    double u;
    double v;
    double rho;
} input_t;

void read_file();

void read_file(){
    int i = 0, j, count = 0;
    int c;
    input_t data[MAX_DATA_NUM];

    FILE *fp;

    fp = fopen ("flow_data.csv", "r");
    if (fp == NULL){
        printf("Error reading file, exiting...");
        exit(EXIT_FAILURE);
    }
    while (i < MAX_DATA_NUM) {
        if(fscanf(fp, "%lf %lf %lf %lf %lf", &data[i].x, &data[i].y, &data[i].u, &data[i].v, 
        &data[i].rho) == 5)
            i++;
        else break;
    }
    for (j = 0; j < i; j++) {
        printf("x: %f, y: %f, u: %f, v: %f, rho: %f \n", data[j].x, data[j].y, 
        data[j].u, data[j].v, data[j].rho);
    }
    for (c = getc(fp); c != EOF; c = getc(fp)) {
        if (c == '\n') {
            count = count + 1;
        }
    }
    fclose(fp);
    printf("The file has %d lines\n ", count);
}

Note that lines containing valid data other than the last line won't be counted because they are consumed by the data-reading part.

MikeCAT
  • 73,922
  • 11
  • 45
  • 70