-3

我正在编写一个关于现金管理系统的程序,但我一直在处理文件。它是一个很长的程序,使用函数将其分解成小块。

问题是我正在创建一个文件,该文件将保留收到的现金记录,我已成功存储数据,并且已使用这些代码创建了文件,现在我想使用函数读取文件的数据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();
}

主要代码是

#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();
}
4

1 回答 1

1

<fstream.h>并且<iostream.h>在 C 中不存在。它们是旧式 C++ 头文件。如果您使用 C,请不要使用它们。

您所需要的只是fscanf函数,它是<stdio.h>. 就像是:

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

格式化程序(或任何其他读取数字的%u格式化程序)将吃掉数字之前的任何行尾、制表符、空格和其他空格。

于 2013-04-08T03:04:05.473 回答