# C Program use LARGE STRUCTURE & ARRAY



## kARTechnology (Jan 23, 2015)

Hi
 completed C subject in my Engineering...
Today I developed a code, *I'm getting array is too large and structure is too large.*but i need large structures, or is there any alternative way?

the aim is to STORE log records(roll,time) from a fingerprint-based time attendance soft which saves data daywise in plain text format..
and compute the salary per day, if he's late/early going/no outpunch etc

*
error is in *

```
struct year
{	struct month
		{
		struct day
			{
			struct details
				{
						char	name[25];
						int	hour[40];
						int	min[40];
						int	mins[40];
						int	rec;
						int	totmins;
						int	totsal;
						char	status;
						int	temp;
				}data[100];
			}day[31];
		}mon[12];
}yr[2];
```

actual full code of my program[in development...

```
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include <time.h>

void dummy(float *a) 
{
    float b=*a; //perform some floating access
    dummy (&b); //calling a floating point function
}


main()
{

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

/*--------- data+details file structure --------*/
struct year
{	struct month
		{
		struct day
			{
			struct details
				{
						char	name[25];
						int	hour[40];
						int	min[40];
						int	mins[40];
						int	rec;
						int	totmins;
						int	totsal;
						char	status;
						int	temp;
				}data[100];
			}day[31];
		}mon[12];
}yr[2];



/*--------- data file structure --------*/
struct details
{ 	int empnr;
	char name[25];
	int salary;
}person[100];




int y1,m1,d1,y2,m2,d2,y,m,d,y3,y4, i=0,noofemp=0,t1,t2,t3;
char fname[20];
clrscr();


printf("Edit \"empdetails.txt\" for adding new or changing roll no, name, salary per day.");

printf("Enter begining date: MM DD YY:");
scanf("%d%d%d",&d1,&m1,&y1);
printf("Enter ending date: MM DD YY:");
scanf("%d%d%d",&d2,&m2,&y2);
if(y1-y2>1)
printf("Cant input greater than years");
printf("Reading Data.");
d=d1;m=m1;y=y1;
//if(y1==y2)
//{y3=0;y4=0;}
//else
//{y3=0;y4=1;}



	FILE *emp_file;
	FILE *calcemp_file;
	emp_file = fopen("emp.txt","r");
	calcemp_file = fopen("calcweek.txt","w");

if(emp_file==NULL)
{
	printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll	Name	   Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
	getch();
	exit(0);
}

/*--------- get data from file --------*/
while(fscanf(emp_file,"%d%s%d", &person[i].empnr, person[i].name,&person[i].salary)==3)
{
i++ ;
noofemp++;
printf(".");
}
fclose(emp_file);


/*--------- get details from file --------*/
for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{
	sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);
	emp_file = fopen(fname,"r");
if(emp_file==NULL)
{	printf("\nERROR Accessing %s for computing salary",fname);
	getch();
	exit(0);
}
	fclose(emp_file);
	printf(".");
}
}
}

getch();
}
```


----------



## SaiyanGoku (Jan 23, 2015)

what compiler/IDE are you using?
Total size in memory is over 20 million bytes for the structure and it will cause unexpected termination.


----------



## kARTechnology (Jan 23, 2015)

SaiyanGoku said:


> what compiler/IDE are you using?
> Total size in memory is over 20 million bytes for the structure and it will cause unexpected termination.



will run the compiled final exe in a windows 8.1 32bit 2gb ram
currently using dosbox (set ram as 64M) with* TC++ v3.0 Borland*


----------



## harshilsharma63 (Jan 24, 2015)

That struct is just too large. What you should do is use buffering technique - fetch a few hundred lined from file, process then, delete them from memory and fetch new lines. Don;t load the whole file into memory at once.


----------



## layzee (Jan 24, 2015)

harshilsharma63 said:


> That struct is just too large. What you should do is use buffering technique - fetch a few hundred lined from file, process then, delete them from memory and fetch new lines. Don;t load the whole file into memory at once.



+1
A divide-and-conquer(ish) approach seems to be the best solution here when there is a constraint on memory availability.


----------



## kARTechnology (Jan 24, 2015)

layzee said:


> +1
> A divide-and-conquer(ish) approach seems to be the best solution here when there is a constraint on memory availability.


*
could you please elaborate, like a eg for the code...do you mean malloc? 
the final output will be like this in a text file, for each employee*

```
Roll no:_1_ name:___test1______salary per day:_____100_____
---------------------------------------------------------------------------------------------------------------
date        | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | total duration | status | salary
21-01-14
22-01-14
23-01-14
                                                                                                                                              total salary______

Roll no:_2_ name:___test2_____salary per day:_____200_____
---------------------------------------------------------------------------------------------------------------
date        | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | in | out | total duration | status | salary
21-01-14
22-01-14
23-01-14
                                                                                                                                              total salary______
```
where in means the time at which he came inside, out means when he went outside.


----------



## harshilsharma63 (Jan 24, 2015)

How many different employees do you have?


----------



## Vyom (Jan 24, 2015)

The way the structure is declared seems to be the culprit to me. The problem with your approach seems to be that you are trying to create n numbers of objects within the same program and all of those are nested.

Assuming that each variable takes following size in memory:

```
char name[25]; (25 bytes)
int	hour[40]; (40*2 = 80 bytes)
int	min[40]; (40*2 = 80 bytes)
int	mins[40]; (40*2 = 80 bytes)
int	rec; (2 bytes)
int	totmins; (2 bytes)
int	totsal; (2 bytes)
char	status; (1 byte)
int	temp; (2 bytes)
```

The total number of memory you are trying to allocate in an object of data = 274 bytes;
data[100] = 27400 bytes.
day[31] = data[100] * 31 = 849400 bytes
mon[12] = day[31] * 12 = 10192800 bytes
year[2] = mon[12] * 2 = 20385600 bytes = *19.44 MB* (!)

As per this page, it seems your structure is very memory intensive, hence crashing.

I think you need to re-think the architecture of the program.
Instead of making multi-level structure you can try following method:


```
struct date
{
	int day, month, year;
};

struct Employee_Info
{
	char	name[25];
	int	hour[40];
	int	min[40];
	int	mins[40];
	int	rec;
	int	totmins;
	int	totsal;
	char	status;
	int	temp;
	date	date_obj;
};

struct Employee_Details
{
	int empnr;
	char name[25];
	int salary;
	Employee_Info empinfo;
};
```


Then you can use the object of Employee_Details like:

```
Employee_Details Emp1, Emp2;
```
Or make an array:

```
Employee_Details Emp[100];
```

Now you can access a particular employees details as:

```
Emp1.empnr;
Emp1.name;
Emp1.salary;
Emp1.empinfo.hour;
...
Emp1.empinfo.date_obj.day;
Emp1.empinfo.date_obj.month;
Emp1.empinfo.date_obj.year;
```


----------



## kARTechnology (Jan 24, 2015)

Vyom said:


> The way the structure is declared seems to be the culprit to me. The problem with your approach seems to be that you are trying to create n numbers of objects within the same program and all of those are nested.
> 
> Assuming that each variable takes following size in memory:
> 
> ...



you almost came close, 

so for example to get the data of an employee such 
the time he came in
---
to get the 1st *hour*/1st fingerprint given time
hours(the time of the day, here hours represent *19*:15) in 24-01-14 date of roll no 2, 

```
year[14].month[1].day[24].emp[2].hours[0][I]
or this way of structure also ok[/I]
emp[2].year[14].month[1].day[24].min[0]
```
---

to get the 1st *minutes*/1st fingerprint given time
minutes(the time of the day, here minutes represent 19:*15*) in 24-01-14 date of roll no 2, 

```
year[14].month[1].day[24].emp[2].min[0][I]
or this way of structure also ok[/I]
emp[2].year[14].month[1].day[24].min[0]
---
```


----------



## aaruni (Jan 25, 2015)

since [MENTION=77264]Vyom[/MENTION] defined his structure as Employee_Details -> Employee_Info -> date ( -> means containing, in this case), you will have to use the structure objects in that order. So,


```
Emp[i].empinfo.date_obj.day
```

Another thing I want to question : why on earth are you doing it on an old, outdated Borland compiler ?


----------



## kARTechnology (Jan 25, 2015)

aaruni said:


> since [MENTION=77264]Vyom[/MENTION] defined his structure as Employee_Details -> Employee_Info -> date ( -> means containing, in this case), you will have to use the structure objects in that order. So,
> 
> 
> ```
> ...


I know only c & dos currently, what should i use?


got the code working: didnt complete the final fprintf part still,,,*but one bug pls help with this*, 

*i58.tinypic.com/2cdbsqr.jpg


Spoiler



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

void dummy(float *a)
{
    float b=*a; //perform some floating access
    dummy (&b); //calling a floating point function
}


/*--------- data+details file structure --------*/

struct day
	{
	int	hour[40];
	int	min[40];
	int	mins[40];
	int	rec;
	int	totmins;
	int	totsal;
	char	status;
	int	temp;
	};
struct month
{	struct day *da;};

struct year
{	struct month *mn;};

struct roll
{
	inr rn;
	char name[30];
	int salary;
	struct year *yr;
};



main()
{

  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );




int    t1=100,t2=25,t3=12,t4=31,    y1,m1,d1,  y2,m2,d2,  y,m,d,  y3,y4, i,j,noofemp=0;
char fname[20],t5[30],t6,t7;
clrscr();

struct roll *rol;
struct year *yr;
struct month *mn;
struct day *da;
  rol=(struct roll*)malloc(t1*sizeof(struct roll));
     if( rol== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  yr=(struct year*)malloc(t2*sizeof(struct year));
     if( yr== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  mn=(struct month*)malloc(t3*sizeof(struct month));
     if( mn== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}
  da=(struct day*)malloc(t4*sizeof(struct day));
     if( da== NULL )
     {printf("Couldn't able to allocate requested memory\n");exit(0);}


printf("Edit \"empdetails.txt\" for adding new or changing roll no, name, salary per day.");

printf("Enter begining date: MM DD YY:");
if ((scanf("%d%d%d",&d1,&m1,&y1)!=3))
{printf("Invalid input");}

printf("\nEnter ending date: MM DD YY:");
if ((scanf("%d%d%d",&d2,&m2,&y2)!=3))
{printf("Invalid input"); }

if(y1-y2>1)
printf("\nCant input greater than 1 year");

d=d1;m=m1;y=y1;
//if(y1==y2)
//{y3=0;y4=0;}
//else
//{y3=0;y4=1;}



	FILE *emp_file;
	FILE *calcsal_file;
	emp_file = fopen("emp.txt","r");
	calcsal_file = fopen("calcsalary.txt","w");

if(emp_file==NULL)
{
	printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll	Name	Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
	getch();
	exit(0);
}

/*--------- get data from file --------*/
printf("\nReading Employee Details.");
while(fscanf(emp_file,"%d%s%d",&t1,&t5,&t3)!=EOF)
{

printf("\nRead %d\t\%s\t%d",t1,t5,t3);
strcpy(rol[t1].name,t5);
rol[t1].salary=t3;
printf("\nRead %d\t\%s\t%d",t1,rol[t1].name,rol[t1].salary);

if(noofemp<t1)
noofemp=t1;

}
printf("\nNumber of employees: %d",noofemp);
for(i=1;i<=noofemp;i++)
printf("\n%d\t\%s\t%d",i,rol_.name,rol.salary);
getch();
fclose(emp_file);

/*--------- get details from file --------*/


for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{
	sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);
	emp_file = fopen(fname,"r");

	if(emp_file==NULL)
	{	printf("\nERROR Accessing %s for computing salary",fname);
		getch();
		exit(0);
	}
		printf("\nReading %s\t",fname);
		getch();

for(i=1;i<=100;i++)
		{
		rol.rn=i;*
		rol.yr[y].mn[m].da[d].rec=0;------------------------>not working as expected, rec is used to denote the no if times in a day he used the fingerprint/put his finger*
		printf("\n rec of  %d is %d    %d",rol.rn,rol.yr[y].mn[m].da[d].rec);
		}
		getch();
		while(fscanf(emp_file,"%d%d%d%d",&t1,&t2,&t3,&t4)!=EOF)
		{
		       printf("\n rec of  %d is %d    %d",t1,rol[t1].yr[y].mn[m].da[d].rec);*

			rol[t1].yr[y].mn[m].da[d].rec++;*
			rol[t1].yr[y].mn[m].da[d].hour[rol[t1].yr[y].mn[m].da[d].rec]=t2;
			rol[t1].yr[y].mn[m].da[d].min[rol[t1].yr[y].mn[m].da[d].rec]=t3;
			rol[t1].yr[y].mn[m].da[d].mins[rol[t1].yr[y].mn[m].da[d].rec]=t4;

			printf("\nRead    %d   %d:%d  %d",t1,t2,t3,t4);
			printf("\nRead(m) %d   %d:%d  %d 	%d 	%s		%d",t1,rol[t1].yr[y].mn[m].da[d].hour[j],rol[t1].yr[y].mn[m].da[d].min[j],rol[t1].yr[y].mn[m].da[d].mins[j],rol[t1].salary,rol[t1].name,rol[t1].yr[y].mn[m].da[d].rec);


		}
		getch();
	fclose(emp_file);
		printf(".");




}
}
}
fprintf(calcsal_file,"\n								name of company");
fprintf(calcsal_file,"\n							       Salary Calculator v3.0					Run on: ");
fprintf(calcsal_file,"\n						 Report From %d-%d-%d to %d-%d-%d",d1,m1,y1,d2,m2,y2);
for(i=1;i<=noofemp;i++)
		{
fprintf(calcsal_file,"\n\n Roll: %d	 Name:%s		Salary/Day:%d",i,rol.name,rol.salary);
fprintf(calcsal_file,"\n -----------------------------------------------------------------------------------------------------------------------------------");
fprintf(calcsal_file,"\n Date   In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out |  In Out | Total Hours | Status | Salary / Day | Salary /Hour   ");
for(;y<=y2;y++)
{
for(;m<=m2;m++)
{
for(;d<=d2;d++)
{

fprintf(calcsal_file,"\n %d-%d-%d",d,m,y);
for(j=1;j<=rol.yr[y].mn[m].da[d].rec;j++)
{fprintf(calcsal_file,"\n %02d:%02d  ",rol.yr[y].mn[m].da[d].hour[j],rol.yr[y].mn[m].da[d].min[j]);}

}
}
}

}

free(rol);
free(yr);
free(mn);
free(da);

getch();
}
_


----------



## Vyom (Jan 25, 2015)

I couldn't understand what output window is showing. However I would like to suggest something.

Since you are reading file till you encounter EOF, I suggest you to enter a blank line in the end of the source file. Without a blank line, your program may not be able to read the last line.


----------



## kARTechnology (Jan 25, 2015)

*Re: C Program use LARGE STRUCTURE &amp; ARRAY*



Vyom said:


> I couldn't understand what output window is showing. However I would like to suggest something.
> 
> Since you are reading file till you encounter EOF, I suggest you to enter a blank line in the end of the source file. Without a blank line, your program may not be able to read the last line.



i think now you can...
*s10.postimg.org/7o12ju9o7/bug.png

- - - Updated - - -

guys?

the thing is if i increment one member, for eg

rollnum=1;y=15;m=1;d=17
roll[1].yr[y].mn[m].da[d].rec++;

it is incrementing all other members too...
i mean it is doing 

roll[2].yr[y].mn[m].da[d].rec++;
roll[3].yr[y].mn[m].da[d].rec++;
roll[4].yr[y].mn[m].da[d].rec++;
roll[5].yr[y].mn[m].da[d].rec++;

why?


----------



## kARTechnology (Jan 28, 2015)

*Re: C Program use LARGE STRUCTURE &amp; ARRAY*

bump...anyone???
*modified it but getting couldn't allocate memory...*

```
for (i = 0; i < 100; i++) {
	  rol[i] = (struct roll * ) malloc(100 * sizeof(struct roll));
	  if (rol[i] == NULL) {
	    printf("%d Couldn't able to allocate requested memory for roll\n", i);
	    getch();
	    exit(0);
	  }
	  for (j = 0; j < 25; j++) {
	    rol[i] - > yr[j] = (struct year * ) malloc(25 * sizeof(struct year));
	    if (rol[i] - > yr[j] == NULL) {
	      printf("%d Couldn't able to allocate requested memory for year\n", i);
	      getch();
	      exit(0);
	    }

	    for (k = 0; k < 12; k++) {
	      rol[i] - > yr[j] - > mn[k] = (struct month * ) malloc(12 * sizeof(struct month));
	      if (rol[i] - > yr[j] - > mn[k] == NULL) {
	        printf("%d Couldn't able to allocate requested memory for month\n", i);
	        getch();
	        exit(0);
	      }

	      for (l = 0; l < 31; l++) {
	        rol[i] - > yr[j] - > mn[k] - > da[l] = (struct day * ) malloc(31 * sizeof(struct day));
	        if (rol[i] - > yr[j] - > mn[k] - > da[l] == NULL) {
	          printf("%d Couldn't able to allocate requested memory for day\n", l);
	          getch();
	          exit(0);
	        }
	      }

	    }

	  }
	}
```


----------



## RCuber (Jan 28, 2015)

sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?

EDIT: And why are you not using classes? were you specifically asked to use Structures?


----------



## Vyom (Jan 28, 2015)

RCuber said:


> sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?
> 
> EDIT: And why are you not using classes? were you specifically asked to use Structures?



Cause he's using "C". And I already suggested using a better structure of structures.
This loop inside loop thingy is never memory efficient and consequently error prone.


----------



## kARTechnology (Jan 29, 2015)

RCuber said:


> sorry couldnt go through the full code/thread, but why have you declared a int array for hours/mins etc?
> 
> EDIT: And why are you not using classes? were you specifically asked to use Structures?



I was taught upto structures...
this is my personal project.
you see fingerprint biometric devices in offices and some places to monitor employee status...the bundled software is crap and doesn't have what i need.
I get a log file daywise from the device like this:
roll, hours:mins, min <----now you understand...sample file looks like...min is nothing but the hours:mins in minute format

filename: 20-01-15.dat

```
01	08	01	00538	
23	08	01	00538	
08	08	01	00538	
12	08	01	00538	
06	08	01	00538	
19	08	01	00538	
04	08	01	00538	
27	08	01	00538	
02	08	01	00538	
11	09	01	00544	
05	09	01	00544	
13	09	01	00563	
03	10	01	00623	
11	10	01	00632	
11	10	01	00643	
12	11	01	00691	
12	11	01	00705	
13	12	01	00767	
23	12	01	00770	
08	12	01	00771	
12	12	01	00773	
13	13	01	00780	
27	13	01	00783	
19	13	01	00802	
23	13	01	00821	
02	14	01	00841	
12	14	01	00843	
11	14	01	00847
```
*
i can do the remaining math myself if i get the structure to store data correctly in respective variables....anyway tell me how you'd use "classes"
or C++ vectors also ok...*


----------



## RCuber (Jan 29, 2015)

Offtopic: I haven't worked on C since college, and that's almost 13 years ago!!. may be I worked a little on embedded C but only for a little time. I'm still not sure how people manage coding without classes!!  

On topic: take a look at this. design - How do you implement a class in C? - Stack Overflow and *www.careercup.com/question?id=14184694

and for the love of god please use functions.


----------



## kARTechnology (Jan 29, 2015)

RCuber said:


> Offtopic: I haven't worked on C since college, and that's almost 13 years ago!!. may be I worked a little on embedded C but only for a little time. I'm still not sure how people manage coding without classes!!
> 
> On topic: take a look at this. design - How do you implement a class in C? - Stack Overflow and *www.careercup.com/question?id=14184694
> 
> and for the love of god please use functions.



i don't like passing values to functions...will do that after whole program is finished


----------



## RCuber (Jan 29, 2015)

kARTechnology said:


> i don't like passing values to functions...will do that after whole program is finished



hehe, you gotta start liking functions if you want to be in software development  everything has to be in function. Which sem are you in anyway?


----------



## harshilsharma63 (Jan 30, 2015)

kARTechnology said:


> i don't like passing values to functions...will do that after whole program is finished



Why? Functions are your friend in making code clutter free.


----------



## kARTechnology (Jan 30, 2015)

harshilsharma63 said:


> Why? Functions are your friend in making code clutter free.



mods pls close thread , i accomplished be using a smaller array by *reading the log file multiple times*...


----------



## Vyom (Jan 30, 2015)

Congo. Please post a snippet of the solution for others in case they stuck with similar situation.


----------



## kARTechnology (Jan 30, 2015)

Vyom said:


> Congo. Please post a snippet of the solution for others in case they stuck with similar situation.


well the output thingy not completely completed but heres the full code...its messy...the final salary summary is not yet completed for each employee

```
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

void dummy(float *a)
{
    float b=*a; //perform some floating access
    dummy (&b); //calling a floating point function
}

main()

{
  time_t rawtime;
  struct tm * timeinfo;

  time ( &rawtime );
  timeinfo = localtime ( &rawtime );

struct employee
	{
	int rn;
	char name[30];
	int salary;
	}emp[100];
struct temp
{
	int	hour[40];
	int	min[40];
	int	mins[40];
	int	rec;
	float	totmins;
	int	totsal;
	char	status[30];
	int	temp;
	}t;

int    t1,t2,t3,t4,    y1,m1,d1,  y2,m2,d2,  y,m,d,  i,j,k,l,noofemp,  beginho,beginm,endh,endm,beginmins,endmins;
char fname[20],t5[30],t6,t7;
clrscr();


	



printf("Edit \"empdetails.txt\" for adding new / changing Roll No, Name, Salary/day.   Name: Max 30 Characters");
getch();
d1;d2;m1;   m2;y1;y2;

printf("Enter begining date: MM DD YY:");
if ((scanf("%d%d%d",&d1,&m1,&y1)!=3))
{printf("Invalid input");}

printf("\nEnter ending date: MM DD YY:");
if ((scanf("%d%d%d",&d2,&m2,&y2)!=3))
{printf("Invalid input"); }

if(y1-y2>1)
	{printf("\nCan't input year period > 1 year");getch(); exit(0);}
else if (m1>12||m2>12)
	{printf("\nCant input month > 12");getch(); exit(0);}
else if (d1>31||d2>31)
	{printf("\nCant input day > 31");getch(); exit(0);}

printf("\nEnter Shift Begin Time: HH MM: ");
scanf("%d%d",&beginho,&beginm);
if(beginho>23)
{printf("\nCan't input HH:%d > 23",beginho);getch(); exit(0);}
if(beginm>59)
{printf("\nCan't input MM:%d > 59",beginm);getch(); exit(0);}
printf("\nEnter Shift End Time: HH MM: ");
scanf("%d%d",&endh,&endm);
if(endh>23)
{printf("\nCan't input HH > 23");getch(); exit(0);}
if(endm>59)
{printf("\nCan't input HH > 59");getch(); exit(0);}
beginmins=(beginho*60)+beginm;
endmins=(endh*60)+endm;

	FILE *emp_file;
	FILE *calcsal_file;
	emp_file = fopen("emp.txt","r");
	calcsal_file = fopen("calcsalary.txt","w");

if(emp_file==NULL)
{
	printf("\nERROR Accessing source file for computing salary.\n\nPlease use a text document with the following info:\n Roll	Name	Salary/Day\n\n\n\nFilename of the text document MUST BE \"empdetails\"\ (without the quotes\) ");
	getch();
	exit(0);
}

/*--------- get data from file --------*/
printf("\nReading Employee Details.");
noofemp=0;

while(fscanf(emp_file,"%d%s%d",&t1,&t5,&t3)!=EOF)
{
emp[t1].rn=t1;
strcpy(emp[t1].name,t5);
emp[t1].salary=t3;

if(noofemp<t1)
noofemp=t1;
}

printf("\nTotal Number of Employees: %d",noofemp);
printf("\nEMPLOYEES LIST:");

for(i=1;i<=noofemp;i++)
printf("\nRoll:%d Name:%-30s Salary/Day:%d",emp[i].rn,emp[i].name,emp[i].salary);

fclose(emp_file);






/*--------- get details from fp log file --------*/
fprintf(calcsal_file,"\n     COMPANY NAME  |  Salary Calculator v3.0  |  Report From %d-%d-%d to %d-%d-%d  |  Begin Time:%02d:%02d  End Time:%02d:%02d  |  Run on: %s",d1,m1,y1,d2,m2,y2,beginho,beginm,endh,endm,asctime(timeinfo));

for(i=1;i<noofemp;i++)
{
fprintf(calcsal_file,"\n\n Roll: %d	 Name:%-30s		Salary/Day:%d   Salary/Hour:%.2f",emp[i].rn,emp[i].name,emp[i].salary,(float)emp[i].salary*60/(endmins-beginmins));
fprintf(calcsal_file,"\n -----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
fprintf(calcsal_file,"\n Date      | In      Out | In      Out | In      Out | In      Out | In      Out | In      Out | In      Out | In      Out | In      Out | In      Out | Tot Hrs |       Status        | Sal Day | Sal Hour |  ");

for(y=y1;y<=y2;y ++)
{
 for(m=m1;m<=m2;m++)
  {
   for(d=d1;d<=d2;d++)
    {
	fprintf(calcsal_file,"\n %02d-%02d-%02d  |",d,m,y);
	for(k=0;k<=20;k++)
			{
			t.hour[k]=0;
			t.min[k]=0;
			t.mins[k]=0;
			}
			t.rec=0;
			t.totmins=0;
			t.totsal=0;
			strcpy(t.status,"");
			j=0;  l=0;

	sprintf(fname,"%02d-%02d-%02d.dat",d,m,y);

	emp_file=fopen(fname,"r");
	if(emp_file==NULL)
			{
			printf("\nERROR Accessing %s for computing salary",fname);
			getch();
			}
	
	printf("\nDate:%s     Employee:%d   Name:%-30s",fname,emp[i].rn,emp[i].name);
	while(fscanf(emp_file,"%d %d %d %d \n",&t1,&t2,&t3,&t4)!=EOF)
		{
			if(emp[i].rn==t1)
			{
			t.hour[j]=t2;
			t.min[j]=t3;
			t.mins[j]=t4;
			t.rec=j;
			printf("\nRoll:%02d Time:%02d:%01d  Mins:%04d  Record:%02d",emp[i].rn,t.hour[j],t.min[j],t.mins[j],t.rec);
			if(t.rec%2==0)
			t.totmins-=t.mins[j];
			else
			t.totmins+=t.mins[j];
			j++;
			}
		}

	fclose(emp_file);
	if(t.rec==0)
	{strcpy(t.status,"ABSENT");}
	else
	{
	if(t.rec%2==0)  			//checking outputnch
		{
		strcpy(t.status,"NO OUT");
		l=t.rec+1;
		t.hour[l]=endh;
		t.min[l]=endm;
		t.mins[l]=endmins;
		t.totmins+=t.mins[l];
			}
		if (t.mins[0]>beginmins)
		strcat(t.status," LATE");
		if (t.mins[t.rec]<endmins)
		strcat(t.status," EARLY");
	}
	for(k=0;k<20;k++)			//printin in outs
		{
		fprintf(calcsal_file," ");
		fprintf(calcsal_file,"%02d:%02d",t.hour[k],t.min[k]);

		if(k%2==1)
		fprintf(calcsal_file," |");
		}

	fprintf(calcsal_file," %06.03f  |",(t.totmins/60));



	fprintf(calcsal_file," %-20s|",t.status);

}
}
}
}

fclose(calcsal_file);
getch();
}
```


----------



## Vyom (Jan 30, 2015)

Great to see you have now removed the nested structures and some other complexities.
We could either get this thread closed, and you can recreate another thread in case of further doubts, *OR* we could leave it open until you resolve your (this) program for good.


----------



## harshilsharma63 (Jan 31, 2015)

If you want to improve your programming skills start commenting and using more descriptive identifiers


----------

