I was tring to write some data in a file from the singly linked list and also read the data from that and append it to singly linked list in the begening of the program.For this i had written two seperate functions .
1. writetofile
2. readfromfile

for appending the data in SLL i had used append function

Everything is working well and good .But i m facing a problem :cry: when i write vast amount of data in the file.Whwn there is almost 500 records , and now i want to write some data its appending in SLL but its failed to be written in the file and as a rsult all the data is lost . I want to know wats the problem ?? Please help me . I m doing something wrong ??

Ps. I know that entering almost 500 records is not a joke to test this program.So i m attaching the file data.txt which i had used to store the records. plzz save this file in c:\cheque :rolleyes:


Here's the code :

# include<stdio.h>
# include<conio.h>
# include<graphics.h>
# include<stdlib.h>
# include<dos.h>
# include<string.h>
# include<math.h>

void writetofile(struct node *);
void readfromfile(FILE *,struct node **);
void append(struct node **,char[],char[],char[],char[],char[],long float,char[]);
void display(struct node **);
int check_tel(char[]);
int check_cheque(char[]);
int checkdate(char[]);
int converttointeger(char[]);

struct node         /* Data structure of list */
{    char telno[8];
     char chequeno[7];
     char bankname[50];
     char paydate[12];
     char chequedate[12];
     long float amount;
     char station[4];
     struct node *link;
};

struct f_node             /* Data structure of file */
{    char telno[8];
     char chequeno[7];
     char bankname[50];
     char paydate[12];
     char chequedate[12];
     long float amount;
     char station[3];
};
struct f_node item; /* Variable to access on the file */

void main()
{
      clrscr();
      struct node *p;  /* Header node */
      p = NULL;      /* Empty List */
      char tel[8],ch[7];
      long float am;
      char bn[50],pd[12],cd[12],st[3];
      char choice;
      FILE *fp;
      fp = fopen("c:\\cheque\\data.txt","r");
      readfromfile(fp,&p);
      do
  {   menu:
      clrscr();
      printf("\n1. Entering Of Records");
      printf("\n2. Printing Of Records");
      printf("\n3. Quit");
      printf("\nEnter The Number Of Your Choice");
      fflush(stdin);
      choice = getch();
      switch(choice)
      {    case '1':

	   printf("\n\" ENTER TELEPHONE NO 99 TO EXIT \"");
	   int i=1,ll;
	   while(1)
	   { 	printf("%d .",i);
		tel:
		printf("\n\" ENTER TELEPHONE NO 99 TO EXIT \"");
		printf("          ");fflush(stdin);
		printf("\nENTER TELEPHONE NO : ");gets(tel);
		if(strcmp(tel,"99")==0)
		{  goto breakwhile;
		}
		ll = check_tel(tel);
		if( ll == 0)
		{  printf("\n\" INVALID TELEPHONE NUMBER. ENTER 7 DIGIT NUMBER OR 99 TO EXIT \"");
		   getch();
		   goto tel;
		}
		cheq:
		printf("\n\" ENTER TELEPHONE NO 99 TO EXIT \"");
		fflush(stdin);
		printf("\nENTER CHEQUE NO : ");gets(ch);
		ll = check_cheque(ch);
		if( ll == 0)
		{  printf("\n\" INVALID ENTRY. ENTER A VALID 6 DIGIT CHEQUE NUMBER  \"");
		   getch();
		   goto cheq;
		}

		fflush(stdin);
		printf("\nENTER BANK NAME : ");gets(bn);

		phdate:
		printf("\n\" ENTER TELEPHONE NO 99 TO EXIT \"");
		printf("          ");
		fflush(stdin);
		printf("\nENTER PAYMENT DATE : ");gets(pd);
		ll = checkdate(pd);
		if( ll == 0)
		{  cprintf("\n\" INVALID ENTRY. ENTER A VALID DATE IN dd/mm/yyyy FORMAT \"");
		   getch();
		   goto phdate;
		}
		chdate:
		printf("\n\" ENTER TELEPHONE NO 99 TO EXIT \"");
		printf("          ");
		fflush(stdin);
		printf("\nENTER CHEQUE DATE : ");gets(cd);
		ll = checkdate(cd);
		if( ll == 0)
		{  printf("\n\" INVALID ENTRY. ENTER A VALID DATE IN dd/mm/yyyy FORMAT \"");
		   getch();
		   goto chdate;
		}
		printf("\nENTER AMOUNT : ");scanf("%lf",&am);
		fflush(stdin);
		sta:
		printf("       ");
		printf("\nENTER STATION : ");gets(st);
		if(strlen(st) > 3 || strlen(st) == 0)
		{  printf("\n\" INVALID ENTRY. ENTER A VALID STATION CODE \"");
		   getch();
		   goto sta;
		}
		append(&p,tel,ch,bn,pd,cd,am,st);
		i++;
	   }
	   breakwhile:
	   writetofile(p);
	   break;

	   case '2':
	   clrscr();
	   display(&p);
	   getch();
	   break;


	   case '3':
	   printf("\nTHANK YOU.BYE");
	   getch();
	   exit(0);
	   break;

	   default:
	   goto menu;
	}
   }while(choice != '3');
      getch();
}


void writetofile(struct node *q)
{     FILE *fp;
      fp = fopen("c:\\cheque\\data.txt","w");

      if(fp==NULL)
      {  printf("File Write Error");
	 getch();
      }

      if(q==NULL)  /* Empty List */
      goto last;

      while( q != NULL)
      {  strcpy(item.telno,q->telno);
	 strcpy(item.chequeno,q->chequeno);
	 strcpy(item.bankname,q->bankname);
	 strcpy(item.paydate,q->paydate);
	 strcpy(item.chequedate,q->chequedate);
	 item.amount=q->amount;
	 strcpy(item.station,q->station);
	 fprintf(fp,"%s\t%s\t%s\t%s\t%s\t%lf\t%s\n",item.telno,item.chequeno,item.bankname,item.paydate,item.chequedate,item.amount,item.station);
	 q=q->link;
      }
      last:
      fclose(fp);
}

void readfromfile(FILE *fp,struct node **q)
{    while(!feof(fp))
     {   fscanf(fp,"%s\t%s\t%s\t%s\t%s\t%lf\t%s\n",&item.telno,&item.chequeno,item.bankname,item.paydate,item.chequedate,&item.amount,item.station);
	 append(q,item.telno,item.chequeno,item.bankname,item.paydate,item.chequedate,item.amount,item.station);
     }
     fclose(fp);
}
void append(struct node **q,char tel[],char ch[],char bn[],char pd[],char cd[],long float am,char st[])
{   struct node *temp,*r;
    temp = *q;
    if(*q==NULL)
    {  temp = (struct node *) malloc(sizeof(struct node));
       strcpy(temp->telno , tel);
       strcpy(temp->chequeno,ch);
       strcpy(temp->bankname,bn);
       strcpy(temp->paydate,pd);
       strcpy(temp->chequedate,cd);
       strcpy(temp->station,st);
       temp->amount = am;

       temp->link=NULL;
       *q = temp;
    }
    else
    {  temp = *q;
       while(temp->link != NULL)
      {   temp=temp->link;
      }
       r = (struct node*)malloc(sizeof(struct node));
       strcpy(r->telno,tel);
       strcpy(r->chequeno,ch);
       strcpy(r->bankname,bn);
       strcpy(r->paydate,pd);
       strcpy(r->chequedate,cd);
       strcpy(r->station,st);
       r->amount = am;

      r->link =NULL;
      temp->link = r;
    }
}

void display (struct node **q)
{  struct node *temp;
   temp=*q;
   writetofile(*q);
   int ll;
   if(*q==NULL)
   {  printf("EMPTY LIST");
      getch();
   }
   int xyz=5,i=1;
   while(temp !=NULL)
   {        gotoxy(2,xyz);printf("%d .",i);
	    gotoxy(8,xyz);printf("%s",temp->telno);
	    gotoxy(18,xyz);printf("%s",temp->chequeno);
	    gotoxy(26,xyz);printf("%s",temp->bankname);
	    gotoxy(42,xyz);printf("%s",temp->paydate);
	    gotoxy(53,xyz);printf("%s",temp->chequedate);
	    gotoxy(64,xyz);printf("%.2lf",temp->amount);
	    gotoxy(73,xyz);printf("%s",temp->station);
	    xyz++;
	    if(xyz==21)
	    {   xyz=4;
		getch();
		clrscr();
		gotoxy(22,23);printf("\" HIT A KEY TO VIEW TEXT PAGE \"");
	     }
	     i++;
	    temp=temp->link;
    }
   
}

           

int check_tel(char tt[])
{   int l,i,count=0;
    l = strlen(tt);
    for(i=0;i<l;i++)
    {  if(tt[i] <= 57 && tt[i] >=48)
       count++;
    }
    if(count==l && l==7)
    return 1;

    else
    return 0;
}

int check_cheque(char tt[])
{   int l,i,count=0;
    l = strlen(tt);
    for(i=0;i<l;i++)
    {  if(tt[i] <= 57 && tt[i] >=48)
       count++;
    }
    if(count==l && l==6)
    return 1;

    else
    return 0;
}

int checkdate(char a[])
{  int l,d,y,logic = 0;char dd[4],mm[4],yy[5];
   l = strlen(a);
   if(l!=10)
   return 0;

   else if(a[2] !='/' && a[5] !='/')
   return 0;

   dd[0]=a[0];dd[1]=a[1];dd[2]='\0';
   mm[0]=a[3];mm[1]=a[4];mm[2]='\0';
   yy[0]=a[6];yy[1]=a[7];yy[2]=a[8];
   yy[3]=a[9];yy[4]='\0';
   d = converttointeger(dd);
   y = converttointeger(yy);



   // Case of months having 31 days

   if(strcmp(mm,"01")==0 || strcmp(mm,"03")==0 || strcmp(mm,"05")==0 || strcmp(mm,"07")==0 || strcmp(mm,"08")==0 || strcmp(mm,"10")==0 || strcmp(mm,"12")==0 )
   {  if( d>=1 && d<=31)
      { logic = 1;
      }
   }
   // Case of Februaury
   else if(strcmp(mm,"02")==0)
   {   if(d>=1 && d<=28)
       { logic =1;
       }
       if(d == 29 && y % 4 == 0)
       { logic=1;
       }
   }
   // Case of months having 30 days
   else if(strcmp(mm,"04")==0 || strcmp(mm,"06")==0 || strcmp(mm,"09")==0 || strcmp(mm,"11")==0)
   {
       if(d>=1 && d<=30)
       {  logic =1;
       }
   }

   return logic;
}

int converttointeger(char str[])
{  int r,p,i,k;
   r=0;
   k=0;
   p = pow(10,(strlen(str)-1));
   for(i=0;i<strlen(str);i++)
   {  k= str[i] - '0';
      r += (k*p);
      p/=10;
   }
   return r;
}

Recommended Answers

All 2 Replies

Everything is working well and good

I can't even get it to compile.

long float amount;

Using types before they are defined. And I don't have graphics.h. (That's the joy of nonstandard code -- fewer folks who can help.)

U can delete the line #include<graphics.h> and for the time being u can use float instead of long float

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.