gpt4 book ai didi

c - 如何在 C 中读取数字以 "\n"分隔的文件

转载 作者:行者123 更新时间:2023-11-30 21:04:09 38 4
gpt4 key购买 nike

我正在现金管理系统上编写一个程序,但我陷入了文件处理的困境。它是一个长程序,通过使用函数将其分解为多个短片段。

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

最佳答案

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

您所需要的只是 fscanf函数,<stdio.h> 的一部分。像这样的东西:

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

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

关于c - 如何在 C 中读取数字以 "\n"分隔的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15870494/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com