Click here to Skip to main content
14,735,978 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

What I have tried:

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


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
Posted
Updated 1-Nov-20 9:12am
v2

I guess that you need a fflush to accomplish that. But use the debugger to find it out.

Why are you two different output functions with the same code? => DRY
   
First off, you don't need this code at all:
else
{
    continue;
}
It does nothing at all since the loop will continue anyway!

Now for the problem you have noticed:
Look at your code:
        do{
            input(fi, place);
...
        }while(fgetc(fi)!=EOF);
        
        ave_age = sum_age/count;

        do{
...
        }while(fgetc(fi)!=EOF);

Since the file pointer doesn't change at all between the first and second loops, and the only way out of the first loop is when you are at the end of the file, your second loop will exit immediately after the first iteration, since it checks for exactly the same thing: the end of the file.
Either store all the results as you round through the first loop and process them in a second, or rewind / close and reopen the file between the loops.

I'd strongly recommend that you start to learn to use the debugger - it can really help when looking for problems like this!
   
v2
Quote:
However, my code only prints the last data into both files. I do not know what is wrong with my code.

Stop guessing, use the debugger and make sure.
#include <stdio.h>
        FILE *fi, *fo1, *fo2;
        fi = fopen(fileR, "r");
        fo1 = fopen(file1W, "w");
        fo2 = fopen(file2W, "w");
        
        do{
            input(fi, place); // Here you are reading fi
            sum_age+=p.age;
            ++count;        
            
        }while(fgetc(fi)!=EOF); // til the EOF
        
        ave_age = sum_age/count;

        do{ // what make you think that you are rereading fi in this loop ?
            // What make you think that there is something to read in fi ?
            if (p.age < ave_age) 
            {   
                output1(fo1, place);
            }       
            else
            {
                continue;
            }
        }while(fgetc(fi)!=EOF);

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900