-3

I am writing a program on Cash Management System and I'm stuck with file handling. Its a long program broken into short pieces by using Functions.

The problem is that I am creating a file which will keep the record of reciept cash, I'm suceesfull in storing the data and the file has been created by using these codes & now i want to read the data of the file using functions in C

#include<fstream.h>
#include<iostream.h>
#include<graphics.h>
#include<conio.h>
#include<stdio.h>
void main()
{
 unsigned long long amt=0;
 char ch;
 do
 {
  clrscr();
  int c=0,i=0,gd=DETECT, gm, x=10, y=52, font = 8;
  initgraph(&gd,&gm,"C:\\TC\\BGI");
  settextstyle(font, HORIZ_DIR, 1);
  for (i=0;i<158;i++)
  {
   printf("_");
  }
  outtextxy(x, y, "The amount of Cash Tendered : ");
  gotoxy(48,5);
  scanf("%llu",&amt);
  FILE *fp;
  fp=fopen("TRECIEPT.TXT","a+");
  while((c=getc(fp))!=EOF);
  fprintf(fp,"%llu",amt);
  fprintf(fp,"\n");
  cout<<"\n Do you want to enter more records: ";
  cin>>ch;
 }
 while (ch=='Y' || ch=='y');
 getch();
}

THE MAIN CODE BEING

#include<graphics.h>
#include<conio.h>
#include<stdio.h>
#include<stdlib.h>

static int den[9]={1000,500,100,50,20,10,5,2,1};

int ch=0;

void cash_management();
//void open_cash();
void transaction();
void total_reciept();
void total_payment();
//void closing_cash();
//void reciept();
//void payment();
//void exchange();

void main()
{
 clrscr();
 cash_management();
}

void cash_management()
{
 clrscr();
 int i=0, gd=DETECT, gm, x=33, y=33, font = 10;
 initgraph(&gd,&gm,"C:\\TC\\BGI");
 settextstyle(font, HORIZ_DIR, 1);
 for (i=0;i<160;i++)
 {
  printf("_");
 }
 outtextxy(x, y, "Welcome To Cash Management System");
 printf("\n\n\n");
 for (i=0;i<160;i++)
 {
  printf("_");
 }
 font=7;
 y=y+100;
 x=70;
 settextstyle(font, HORIZ_DIR, 1);
 outtextxy(x, y, "1). Opening Cash Denomination");
 y=y+30;
 outtextxy(x, y, "2). Transactions");
 y=y+30;
 outtextxy(x, y, "3). Total Recipts");
 y=y+30;
 outtextxy(x, y, "4). Total payments");
 y=y+30;
 outtextxy(x, y, "5). Closing Cash Denomination");
 y=y+30;
 outtextxy(x, y, "6). Exit");
 printf("\n\n\n\n\n\n\n\n\n\n\n\n\n");
 for (i=0;i<160;i++)
 {
  printf("_");
 }
 x=190;
 y=y+74;
 font=6;
 settextstyle(font, HORIZ_DIR, 1);
 outtextxy(x, y, "Enter option to perform => ");
 gotoxy (52,24);
 do
 {
  scanf("%d",&ch);
 }
 while (ch<1 || ch>6);
 switch (ch)
 {
  case 1:
     {
      printf("case 1"); //open_cash();}
      break;
     }
  case 2:
     {
      transaction();
     }
  case 3:
     {
      total_reciept();
      break;
     }
  case 4:
    {
     total_payment();
     break;
    }
  case 5:
    {
     printf("case 5");//closing_cash();
     break;
    }
  case 6:
    {
     exit(0);
    }
 }
}

void transaction()
{
 clrscr();
 int i=0, j=0, gd=DETECT, gm, x=200, y=33, font = 10;
 initgraph(&gd,&gm,"C:\\TC\\BGI");
 settextstyle(font, HORIZ_DIR, 1);
 for (i=0;i<2;i++)
 {
  printf("\t\t\t");
  for (j=0;j<24;j++)
  {
   printf("_");
  }
  printf("\n");
 }
 outtextxy(x, y, "Transactions");
 printf("\n\n\n");
 for (i=0;i<2;i++)
 {
  printf("\t\t\t");
  for (j=0;j<24;j++)
  {
   printf("_");
  }
  printf("\n");
 }
 x=33;
 font=8;
 settextstyle(font, HORIZ_DIR, 1);
 y=y+100;
 outtextxy(x, y, "1). Reciept");
 y=y+30;
 outtextxy(x, y, "2). Payment");
 y=y+30;
 outtextxy(x, y, "3). Exchange");
 y=y+30;
 outtextxy(x, y, "4). Back");
 printf("\n\n\n\n\n\n\n\n\n");
 for (i=0;i<160;i++)
 {
  printf("_");
 }
 y=y+73;
 outtextxy(x, y, "Enter option to perform =>  ");
 gotoxy(42,20);
 do
 {
  scanf("%d",&ch);
 }
 while(ch<1 || ch>6);
 switch (ch)
 {
  case 1:
     {
      printf("case 1");
//    reciept();
      break;
     }
   case 2:
     {
      printf("case 2");
   //     payment();
      break;
     }
   case 3:
     {
      printf("case 3");
   //     exchange();
      break;
     }
   case 4:
     {
      cash_management();
      break;
     }
  }
}

void total_reciept()
{
 unsigned long long amt=0,t_amt=0;
 clrscr();
 int c=0,i=0,gd=DETECT, gm, x=10, y=52, font = 8;
 initgraph(&gd,&gm,"C:\\TC\\BGI");
 settextstyle(font, HORIZ_DIR, 1);
 for (i=0;i<158;i++)
 {
  printf("_");
 }
 outtextxy(x, y, "The Amount of Cash Tendered are : ");
 gotoxy(48,6);
 FILE *fp;
 fp=fopen("TRECIEPT.TXT","a+");
 i=1;
 while((c=getc(fp))!=EOF)
 {
  fscanf(fp,"%llu",&amt);
  printf("\n %d",i);
  printf("   %llu",amt);
  t_amt=t_amt+amt;
  i++;
 }
 printf("\n\n The Total Amount of cash tendered is:  %llu",t_amt);
 getch();
 cash_management();
}

void total_payment()
{
 unsigned long long amt=0,t_amt=0;
 clrscr();
 int c=0,i=0,gd=DETECT, gm, x=10, y=52, font = 8;
 initgraph(&gd,&gm,"C:\\TC\\BGI");
 settextstyle(font, HORIZ_DIR, 1);
 for (i=0;i<158;i++)
 {
  printf("_");
 }
 outtextxy(x, y, "The Amount of Cash Paid are : ");
 gotoxy(48,6);
 FILE *fp;
 fp=fopen("TPAYMENT.TXT","a+");
 i=1;
 while((c=getc(fp))!=EOF)
 {
  fscanf(fp,"%llu",&amt);
  printf("\n %d",i);
  printf("   %llu",amt);
  t_amt=t_amt+amt;
  i++;
 }
 printf("\n\n The Total Amount of cash Paid is:  %llu",t_amt);
 getch();
 cash_management();
}
genpfault
  • 51,148
  • 11
  • 85
  • 139
  • 1
    What exactly is the problem you are experiencing? Your code seems to have a number of issues. – GraphicsMuncher Apr 08 '13 at 02:55
  • I would like to have a total of reciept and payment file which would store all the reciepts of a particular day and calculate the total amount recieved or paid. Moreover I would like to remove the """initgraph(&gd,&gm,"C:\\TC\\BGI");""" As this code requires the C compiler to be installed. – Vipul Sonker Apr 08 '13 at 02:58
  • Get rid of your conflicting definitions of `main` and properly format your code - it will make it easier for people to help you. – GraphicsMuncher Apr 08 '13 at 03:12

1 Answers1

1

<fstream.h> and <iostream.h> don't exist in C. They're old-style C++ headers. Don't use them if you're using C.

All you need is the fscanf function, part of <stdio.h>. Something like:

fscanf(fp, "%llu", &amt);

The %u formatter (or any other number-reading formatter) will eat any line ends, tabs, spaces, and other whitespace that precedes the number.

Mike DeSimone
  • 41,631
  • 10
  • 72
  • 96
  • then how shall i differentiate the numbers if it will eat line ends. suppose the reciept file contains amount as:- 123456 1234567 1234567 1234567 12345678 123456789 Then how shall i get the amount and add them to get the total amount – Vipul Sonker Apr 08 '13 at 03:18
  • The first time you call `fscanf` you'll get 123456. The second time, you'll get 1234567, and so on. Each call gets you one number. If that's not enough, then your file doesn't contain enough information. – Mike DeSimone Apr 08 '13 at 12:38
  • I'm only willing to solve specific problems, not address design issues like that. – Mike DeSimone Apr 08 '13 at 12:47
  • Not a clue. I don't do Windows. – Mike DeSimone Apr 08 '13 at 13:55
  • Is there a way to go to a specific line in a file discussed above and modify the line. The total number of line in a file being only 9 and storing only one number in one line of 1 to 6 digit – Vipul Sonker Apr 08 '13 at 17:27
  • Not easily. Generally you recreate the whole file. – Mike DeSimone Apr 08 '13 at 20:38