Hello Guys,
I am trying to read lines from .xml file using a C program.
Basically what program does is read the date, and if the current system date is in the range of the date range given, it fetches some data from the next lines in the date.

However,my program is only reading till 240.
I am using fgets function to read line by line till I find the date.
When I find a date, I compare it and if in range, fetch the data below that date.

The program works perfectly till 240 lines, but after that its just terminating itself.
The date after the date on the 240th line is the one it should fetch data for.
So I made the program output every line it was reading and the program terminates on 240th line. But if I change the date of the 240th line to be valid, it still fetches the data perfectly.

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <time.h>
#include <math.h>
	
FILE *fh;	
char line[200]; 
int num_start[3],num_end[3];

// Fetch function fetches the data below the date, if the date is valid.

int fetch(int Day, int Month, int Year)
{
	char email1[20],email2[20];
	while(fgets(line, sizeof(line),fh))
	{
		if(line[2]=='<' && line[3]=='j' && line[4]=='u' && line[5]=='m' && line[6]=='p')
		break;
	}
	
	int k=0;
	int b1=0;
	int b2=0;
	while(line[k]!='\0')
	{
		if(line[k]=='(')
		{
			b1=k;
		}
		else if(line[k]==')')
		{
			b2=k;
		}
		k++;
	}

        int c=0;
	int d=0;
	printf("E-Mail 1 = ");
	for(c=b1+1;c<b2;c++)
	{
		email1[d]=line[c];
		printf("%c",email1[d]);
		d++;
	}
	email1[d]='\0';
	printf("\n");
	
	while(fgets(line, sizeof(line),fh))
	{
		if(line[2]=='<' && line[3]=='j' && line[4]=='u' && line[5]=='m' && line[6]=='p')
		break;
	}

	k=0;
	b1=0;
	b2=0;

	while(line[k]!='\0')
	{
		if(line[k]=='(')
		{
			b1=k;
		}
		else if(line[k]==')')
		{
			b2=k;
		}
		k++;
	}

	c=0;
	d=0;
	printf("E-Mail 2= ");
	for(c=b1+1;c<b2;c++)
	{
		email2[d]=line[c];
		printf("%c",email2[d]);
		d++;
	}
	email2[d]='\0';
	printf("\n");
	
	

return 0;

}

// comparing day.

int comp_day(int Day, int Month, int Year)
{

	if(Day>=num_start[0] && Day<=num_end[0])
	{
		fetch(Day,Month,Year);
	}
	else if(Day<num_start[0] || Day<num_end[0])
	{
		read_file();
	}

return 0;
}

//comparing month.

int comp_month(int Day, int Month, int Year)
{

	if(Month>num_start[1] && Month<num_end[1])
	{
		
		fetch(Day,Month,Year);
	}

	else if(Month==num_start[1] && Month<num_end[1])
	{
		if(Day>=num_start[0])
		{
			
			fetch(Day,Month,Year);
		}
		else if(Day<num_start[0])
		{
			read_file();
		}
	}

	else if(Month>num_start[1] && Month==num_end[1])
	{
		if(Day<=num_end[0])
		{
			fetch(Day,Month,Year);
		}
		else if(Day>num_start[0])
		{
			read_file();
		}
	}

	else if(Month==num_start[1] && Month==num_end[1])
		{
			comp_day(Day,Month,Year);
		}
	
	else if(Month<num_start[1] || Month>num_end[1])
		{
			read_file();
		}

return 0;
}

//Comparing year

int comp_year(int Day, int Month, int Year)
{ 
	
	if(Year>num_start[2] && Year<num_end[2])
	{
	fetch(Day,Month,Year);
	}
	
	else if(Year==num_start[2] && Year<num_end[2])
	{
		if(Month>num_start[1])
		{
			fetch(Day,Month,Year);
		}
		else if(Month==num_start[1])
		{
			if(Day>=num_start[0])
			{
				fetch(Day,Month,Year);
			}
			else if(Day<num_start[0])
			{
				read_file();
			}
		}
		else if(Month<num_start[1])
		{
			read_file();
		}
	}

	else if(Year>num_start[2] && Year==num_end[2])
	{
		if(Month<num_end[1])
		{
			fetch(Day,Month,Year);
		}
		else if(Month==num_end[1])
		{
			if(Day<=num_end[0])
			{
				fetch(Day,Month,Year);
			}
			else if(Day>num_end[0])
			{
				read_file();
			}
		}
		else if(Month>num_end[1])
		{
			read_file();
		}

	}

	else if(Year==num_start[2] && Year==num_end[2])
	{
		comp_month(Day,Month,Year);
	}

	else if(Year<num_start[2] || Year>num_end[2])
	{
		read_file();
	}

return 0;

}

// Keeps reading the file until a valid date is found or EOF reached.

int read_file(void)
{

	
int i=0;

while(fgets(line, sizeof(line), fh))
	{

	i=isdigit(line[6]);
	printf("%s\n",line);         //terminates after reading 240th line
	
	if(i!=0)
		{
		break;
		}
	}

if(feof(fh))
	{
		return 1;
	}

// Since its reading characters, I made seperate for each date component.
char day_start[3],month_start[3],year_start[5];
char day_end[3],month_end[3],year_end[5];
day_start[2]='\0';
month_start[2]='\0';
year_start[4]='\0';
day_end[2]='\0';
month_end[2]='\0';
year_end[4]='\0';

		
// XML file has a standard format for dates, so inputting was easy after the line was read.
day_start[0]=line[6];    
day_start[1]=line[7];
month_start[0]=line[9]; 
month_start[1]=line[10];
year_start[0]=line[12];
year_start[1]=line[13];
year_start[2]=line[14];
year_start[3]=line[15];
day_end[0]=line[19];    
day_end[1]=line[20];
month_end[0]=line[22];    
month_end[1]=line[23];
year_end[0]=line[25];
year_end[1]=line[26];
year_end[2]=line[27];
year_end[3]=line[28];		




// removing date padding 0's

if(day_start[0]=='0')
	{
		day_start[0]=day_start[1];
		day_start[1]='\0';
	}

if(day_end[0]=='0')
	{
		day_end[0]=day_end[1];
		day_end[1]='\0';
	}
if(month_start[0]=='0')
	{
		month_start[0]=month_start[1];;
		month_start[1]='\0';
		
	}
if(month_end[0]=='0')
	{
		month_end[0]=month_end[1];
		month_end[1]='\0';
		
	}



//converting string to Integer
num_start[1]=atoi(day_start);
num_start[0]=atoi(month_start);
num_start[2]=atoi(year_start);
num_end[1]=atoi(day_end);
num_end[0]=atoi(month_end);
num_end[2]=atoi(year_end);





// fetching system date.
int Day,Month,Year;
struct tm *Sys_T = NULL;
time_t Tval = 0;
Tval = time(NULL);
Sys_T = localtime(&Tval);
Day=Sys_T->tm_mday;
Month=Sys_T->tm_mon+1;
Year=1900 + Sys_T->tm_year;


//calling compare function.
comp_year(Day,Month,Year);



return 0;

}



int main(int argc, char * argv[])
{

if(argc < 2)
	{
	printf("Need file name.\n");
	return -1;
	}


fh=fopen(argv[1] , "r");
	
if(fh==NULL)
	{
	printf("Cannot open file.\n");
	return -1;
	}

read_file();

fclose(fh);
return 0;
}

I am using Ubuntu's integrated GCC compiler to compile and then run.

Is there any limitation that a program can only read 240 lines from a file?

Thanks.

EDIT: Didn't see the problem was solved, apologies.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.