0

Im trying to store ints and floats from a file to an array. Im not sure why its not reading the values. It just keeps giving me all 0s. I tried with a different .txt file(same format) but I got the same result. I thought this would work but i cant figure out why it wont work. This is what I currently have:

int main(int argc, char *argv[]){
struct TData{
    int MinIndex,MaxIndex,Points;
    float *pValue;
    } Input[2],Output;
    FILE *fp;
    char ch;
    int i,*values;
    int lines=0;
    fp = fopen(argv[1],"r");
    if(fp == NULL){
            printf("dont do anything\n");
    }
    else{
            while(!feof (fp)){
                    ch = fgetc(fp);
                    if(ch == '\n'){
                            lines++;
                    }
            }
            values = malloc(sizeof(int) * (lines));
            Input[0].pValue = malloc(sizeof(float) * (lines));
            for(i=0;i<lines;i++){
                    fscanf(fp, "%d %f",&values[i],&Input[0].pValue[i]);
                    printf("%d  %f\n\n",values[i],Input[0].pValue[i]);

            }
    }

What the file contains:

3   3.0000000
2   2.0000000
1   1.0000000

The output I am getting:

0   0.0000000
0   0.0000000
0   0.0000000
  • Perhaps the previous Q [fscanf not reading floats correctly](https://stackoverflow.com/questions/14618369/fscanf-not-reading-floats-correctly) can help. Also, please see [Why is `while ( !feof (file) )` always wrong?](http://stackoverflow.com/questions/5431941/why-is-while-feof-file-always-wrong) And, make sure you use the correct variable type with i/o functions - they usually need `int` and not `char` as advised in the man page for the functions you use. – Weather Vane Mar 04 '18 at 17:43
  • @WeatherVane Corrected the while statement to checking if the number is EOF, and I looked at the float link you linked I check for those problems. And I dont have any of those problems. – Kishan Patel Mar 04 '18 at 17:52
  • @WeatherVane I corrected to that but it will gave me the same output – Kishan Patel Mar 04 '18 at 17:56
  • (Edit to deleted comment) Better to check the number of arguments that were converted. – Weather Vane Mar 04 '18 at 17:56
  • 2
    You read to EOF, then expect to read more data without rewinding the file. It won’t work like that. – Jonathan Leffler Mar 04 '18 at 17:59
  • If a file consisted of only 3 characters `"abc"` and no `'\n'`, how many lines is that 0 or 1? – chux - Reinstate Monica Mar 04 '18 at 21:45
  • @chux I think it would be 1 line – Kishan Patel Mar 04 '18 at 22:02
  • @JonathanLeffler Thank you, i figured it out! – Kishan Patel Mar 05 '18 at 14:24

1 Answers1

0

This is a basic example showing how to read the file.

#include <stdio.h>

int main(int argc, char *argv[]){
    FILE *fp;
    int ivalue;
    float fvalue;

    if(argc < 2) {
        return 1;
    }

    fp = fopen(argv[1], "r");
    if(fp == NULL) {
        return 1;
    }

    while(fscanf(fp, "%d%f", &ivalue, &fvalue) == 2) {
        printf("%-5d %9f\n", ivalue, fvalue);
    }

    fclose(fp);
    return 0;
}

Program output

3      3.000000
2      2.000000
1      1.000000

Note that fscanf format specifiers %d and %f both ignore any whitespace so in this case it is unnecessary to mess around finding newlines.

Weather Vane
  • 33,872
  • 7
  • 36
  • 56