Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
A text file contains a list of names,age and gender of 10 people.You are required to find who has the the minimum age and print the same.
Contents of a text file:
suresh		10	male 
ram		20	male
sita		20	female
paresh		25	male
anish		18	male
rajasree	30	female
gita		35	female
asutosh		28	male
shubham		21	male
suman		39	male


Output:
Suresh is the youngest whose age is 10

What I have tried:

Unfinished code:
C++
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include<ctype.h>
int i,j,temp;
struct
{
    char name[40];
    int age;
    char sex[10];
}person[9];
void main()
{
    FILE *fp;
    fp=fopen("candidate.txt","r");
    char a[10];
    int val;
    if((fp=fopen("candidate.txt","r"))==NULL)
        {
            printf(" ! The File is Empty...\n\n");
        }
    char ch;
    for(i=0;i<10;i++)
    {
     do
     {
        fscanf(fp,"%s %d %s",person[i].name,&person[i].age,person[i].sex);
        while((ch=fgetc(fp))!='\n')
        {
         if(isdigit(ch)!=0 || isspace(ch)!=0)
           {
            if(fscanf(fp,"%d",&person[i].age)==ch)
            {
              for(j=i+1;j<10;j++)
               a[i]=ch;
               if(a[i]<a[j])
               {
                   temp=i;
               }
             //printf("%d",c);
           }
           }
        }
     }while((fgetc(fp))!=EOF);
      /*for(i=0;i<10;i++)
        printf("%d ",person[i].age);*/
     }
    printf("Minimum age is:%d",temp);
}
Posted
Updated 25-Jun-18 22:53pm
v2

1. Read the file into an array of structs: Read file into array of structs - C example[^]
2. Sort the array: qsort, qsort_s - cppreference.com[^]
3. Pick the first item...
 
Share this answer
 
Comments
Member 13887243 26-Jun-18 3:51am    
Thank you for your help.
You don't need to collect the items (i.e. you don't need the array), you find the minimum aged person on the fly:
(beware, the following code must be improved for reliability)
C
#include <stdio.h>
#include <string.h>
#include <limits.h>

struct Person
{
  char name[40];
  int age;
  char sex[10];
};


int  main()
{

  struct Person current, youngest;

  youngest.age = INT_MAX;

  FILE *fp;

  int rc = -1;

  fp = fopen("candidate.txt","r");

  if ( ! (fp =fopen("candidate.txt","r")))
  {
    printf("Unable to open the input file...\n\n");
    return -1;
  }

  for(;;)
  {
    int rc = fscanf(fp,"%s %d %s", current.name, ¤t.age, current.sex);
    if ( rc != 3 ) break;
    if ( youngest.age > current.age )
    {
      youngest.age = current.age;
      strcpy(youngest.name, current.name);
      strcpy(youngest.sex, current.sex);
    }
  }

  if ( youngest.age != INT_MAX)
  {
    printf("%s is the yougest whose age is %d\n", youngest.name, youngest.age);
    rc = 0;
  }
  else
  {
    printf("Unable to retrieve data from the file\n");
  }
  fclose(fp);
  return rc;
}
 
Share this answer
 
Comments
Member 13887243 26-Jun-18 5:00am    
I got it.Thank you.

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



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