The file suppose to search the Monkey.dat file and when it finds a monkey that is less then 12 months old, it moves it to cage No.100 (m.cage=100)
and decreases the number of monkeys the caregiver takes care of (c.numMonkey--;)
after the programm makes the changes, it suppose to print every caregiver that has no monkeys.
#include<stdio.h>
#include<stdlib.h>
struct monkey
{
int age; /*mokeys age*/
int care;/* caregivers ID*/
int cage;/*number of cage the monkey's at*/
};
struct care
{
int id;/*caregivers ID*/
int numMonkey; /*number of monkeys*/
};
void clear(FILE *fm, FILE *fc);
void main()
{
struct monkey m;
struct care c;
FILE *monkey, *cares;
int i;
monkey=fopen("H:\\Prog\\C\\files\\Monkey.dat","wb");
cares=fopen("H:\\Prog\\C\\files\\Cares.dat","wb");
printf("Entering data for Monkey.dat...\n");
for(i=0;i<3;i++)
{
printf("\nEnter age of the monkey in months: ");
scanf("%d", &m.age);
printf("Enter the cage number: ");
scanf("%d", &m.cage);
printf("Enter the caregiver ID: ");
scanf("%d", &m.care);
fwrite(&m,sizeof(struct monkey),1,monkey);
}
printf("\n/**************************************************/\n");
printf("\n\nEntering data for Cares.dat...\n\n");
for(i=0;i<2;i++)
{
printf("\nEnter caregiver ID: ");
scanf("%9d", &c.id);
printf("Enter caregivers number of monkeys: ");
scanf("%d", &c.numMonkey);
fwrite(&c, sizeof(struct care),1,cares);
}
fclose(cares);
fclose(monkey);
clear(monkey,cares);
monkey=fopen("H:\\Prog\\C\\files\\Monkey.dat","r+b");
cares=fopen("H:\\Prog\\C\\files\\Cares.dat","r+b");
while(fread(&c,sizeof(struct care),1,cares))
{
if(c.numMonkey<=0)/*If the caregiver has no monkeys to take care of*/
{
printf("Worker number %d has no monkeys to take care of!!!\n", c.id);
}
}
fclose(cares);
fclose(monkey);
}
void clear(FILE *fm, FILE *fc)
{
int ok;/*A check parameter to see if we updated the 'Cares.dat' file*/
struct monkey m;
struct care c;
fm=fopen("H:\\Prog\\C\\files\\Monkey.dat","r+b");
fc=fopen("H:\\Prog\\C\\files\\Cares.dat","r+b");
while(fread(&m,sizeof(struct monkey),1,fm))
{
if(m.age<12)/*if a monkeys age is less then 12 months*/
{
m.cage=100;/*Moving the monkey to cage No.100*/
fseek(fm, -sizeof(struct monkey), SEEK_CUR);/*Going one structure up in the 'Monkey.dat' file*/
fwrite(&m, sizeof(struct monkey),1,fm);/*Updating the cage number in the Monkey.dat file*/
fseek(fc,0,SEEK_SET);/*Going to the start of the file every time we want to scan it*/
ok=0;/*Updatig the value*/
while(fread(&c,sizeof(struct care),1,fc) && !ok)/*While we haven't gptten to the end of file 'Cares.dat'*/
{
if((m.care==c.id)&&(c.numMonkey>0))
{
c.numMonkey--;
fseek(fc,-sizeof(struct care),SEEK_CUR);/*Going one structure up in the 'Cares.dat' file*/
fwrite(&c,sizeof(struct care),1,fc);/*Updating the nu,ber of monkeys the caregiver takes care of*/
ok=1;/*Updated!*/
}
}
}
}
fclose(fc);
fclose(fm);
}
In the clear function, there is an endless loop in the first while.
I've been over this code through and through with and without the Debugger but I can't figure out why =(
This post has been edited by Lizika: 29 November 2009 - 03:14 AM

New Topic/Question
Reply




MultiQuote





|