我正在 现金管理系统 上编写一个程序,我一直在处理文件。 它是一个很长的程序,通过使用函数分成短片。
问题在于我正在创建一个文件,该文件将记录收到的现金,我已经成功存储数据,并且使用这些代码创建了文件。现在我想在 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();
}
答案 0 :(得分:1)
<fstream.h>
和<iostream.h>
。它们是旧式的C ++标题。如果您使用C,请不要使用它们。
您需要的只是fscanf
函数,<stdio.h>
的一部分。类似的东西:
fscanf(fp, "%llu", &amt);
%u
格式化程序(或任何其他数字读取格式化程序)将占用数字前面的任何行结尾,制表符,空格和其他空格。