0

I am writing a C code that will read the data from a text file which contains the following data: name, age, income

After reading the data successfully, the data will be separated into two different files: In the first one all of the entries whose age is below the average In the second one all of the entries whose income is above the average

However, my code only prints the last data into both files. I do not know what is wrong with my code.

MY CODE

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

typedef struct 
{
    char name[15];
    int age;
    float income;
} data;

void input(FILE*, data*);
void output1(FILE*, data*);
void output2(FILE* file2, data* data);

int main(void) {
        data p;
        data *place = &p;
        char fileR[10] = "F1.txt", file1W[10] = "F2.txt", file2W[10] = "F3.txt";
        float ave_income = 0, sum_income = 0; 
        int ave_age = 0, sum_age = 0, count = 0;
        
        FILE *fi, *fo1, *fo2;
        fi = fopen(fileR, "r");
        fo1 = fopen(file1W, "w");
        fo2 = fopen(file2W, "w");
        
        do{
            input(fi, place);
            sum_age+=p.age;
            ++count;        
            
        }while(fgetc(fi)!=EOF);
        
        ave_age = sum_age/count;
        
        
        do{
            if (p.age < ave_age) 
            {   
                output1(fo1, place);
            }       
            else
            {
                continue;
            }
        }while(fgetc(fi)!=EOF);
        
        do{
            input(fi, place);
            sum_income+=p.income;       
            
        }while(fgetc(fi)!=EOF);
        
        ave_income = sum_income/count;
        
        
        do{
            if (p.income > ave_income) 
            {   
                output2(fo2, place);
            }       
            else
            {
                continue;
            }
        }while(fgetc(fi)!=EOF);
    
fclose(fi);
fclose(fo1);    
fclose(fo2);

return 0; 
}
        
void input(FILE* f, data* data)
{
    fscanf(f, "%s %d %f", data->name,&data->age,&data->income);
}
     
void output1(FILE* file, data* data)
{
    
    fprintf(file, "Name %s \nAge: %d\nWind Income: %f \n", data->name, data->age, data->income ); //prints to the file
    printf("Name %s \nAge: %d\nIncome: %f \n",data->name, data->age, data->income); //prints
}

void output2(FILE* file2, data* data)
{
    
    fprintf(file2, "Name %s \nAge: %d\nWind Income: %f \n", data->name, data->age, data->income ); //prints to the file
    printf("Name %s \nAge: %d\nIncome: %f \n",data->name, data->age, data->income); //prints
}

THE TEXT FILE

Alex    25  670
John    24  830
Jessica 31  800
Milana  27  680
Kaarel  28  1200
Triin   34  1300
Maria   29  1450
Jaanus  28  1350
Maaris  26  1100
  • After the first loop reads all the input, later loops repeat the same EOF check instead of using `count`. – Milag Nov 01 '20 at 17:52
  • You mean, I should use "count" instead of using EOF after the first loop – MrsDalloway Nov 01 '20 at 17:59
  • Have you tried running your code line by line in a debugger while monitoring the values of all variables, in order to determine at which point your program stops behaving as intended? If you did not try this, then you probably want to read this: [What is a debugger and how can it help me diagnose problems?](https://stackoverflow.com/q/25385173/12149471) You may also want to read this: [How to debug small programs?](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Andreas Wenzel Nov 01 '20 at 18:16
  • Have you considered using an array of struct to read all data from your input file? That way you have all data stored and do not need to read your input file twice. With a small number of inputs it doesn't make a big difference, but if you had a million lines of input, you wouldn't want to have to read the input more than once. – David C. Rankin Nov 01 '20 at 18:31
  • 1) This is inside `input` function , you are supposed to read from `stdin` instead of `fp` , `fscanf(f, "%s %d %f", data->name,&data->age,&data->income);` ==> `fscanf(stdin, "%s %d %f", data->name,&data->age,&data->income);` 2) the way you are reading the contents in `do while(fgetc(fi)!=EOF)` is wrong 3) you have to use array of strucutres(or with a pointer to a structure) to store the details – csavvy Nov 01 '20 at 18:36
  • sorry miss-interpreted the first one..ignore first comment – csavvy Nov 01 '20 at 18:51

0 Answers0