2 Replies - 574 Views - Last Post: 29 November 2009 - 08:03 AM Rate Topic: -----

#1 Lizika  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-November 09

Problem in C I/O files

Posted 29 November 2009 - 03:12 AM

Hi! I'm having a problem with my code. When I compile the program, it gets stuck in the middle and I can't figure out why.

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


Is This A Good Question/Topic? 0
  • +

Replies To: Problem in C I/O files

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6049
  • View blog
  • Posts: 23,474
  • Joined: 23-August 08

Re: Problem in C I/O files

Posted 29 November 2009 - 07:44 AM

Are you receiving any errors? Does this code not work that way you intended it? When asking for help there are a couple items that are vital in order for someone to properly help you:
  • Post the code you're having problems with
  • Post the exact error you're receiving, if you are receiving one
  • If no error explain what the code is doing versus what you want it to do
  • Post your question in the body of your post, not the description field

Was This Post Helpful? 0
  • +
  • -

#3 Ancient Dragon  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 81
  • View blog
  • Posts: 679
  • Joined: 19-July 09

Re: Problem in C I/O files

Posted 29 November 2009 - 08:03 AM

>> void main()

main() returns int, not void. Read this article


>>When I compile the program, it gets stuck in the middle
You better use a different compiler :crazy:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1