Lúc tạo tài khoản thì nó lưu vào tập tin .txt, nhưng thoát chương trình ra vào lại thì vào đọc tài khoản không đc
Code:
//khai bao thu vien
#include<conio.h>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100
//khai bao cau truc
typedef struct taikhoan{
long sohieu;
long cmnd;
char ten[31];
float sotien;
float laisuat;
}TK;
typedef struct nganhang{
taikhoan tk[MAX];
int n;
}NH;
//nhap 1 tai khoan
void nhaptaikhoan(TK &tk)
{ float temp;
printf("Nhap so hieu: ");
scanf("%ld",&tk.sohieu);
printf("Nhap ho & ten: ");
fflush(stdin);
gets(tk.ten);
printf("Nhap so CMND: ");
scanf("%ld",&tk.cmnd);
printf("Nhap so tien: ");
scanf("%f",&temp);
tk.sotien=temp;
printf("Nhap lai suat: ");
scanf("%f",&temp);
tk.laisuat=temp;
}
//nhap nhieu tai khoan
void nhap(NH &nh,int &d)
{
nh.n=d+1;
for(int i=d;i<nh.n;i++)
nhaptaikhoan(nh.tk[i]);
d++;
}
//xuat 1 tai khoan
void xuattaikhoan(TK tk)
{
printf("\t*----------------------------------*");
printf("\n\tSo tai khoan: %ld\n",tk.sohieu);
printf("\tTen chu tai khoan: ");
puts(tk.ten);
printf("\tSo CMND: %ld\n",tk.cmnd);
printf("\tSo tien: %0.2f vnd\n",tk.sotien);
printf("\tLai suat la: %0.2f\%\n",tk.laisuat);
printf("\t*----------------------------------*");
}
//tim vi tri
int tim(NH nh,long ma)
{
for( int i=0;i<nh.n;i++)
if(nh.tk[i].sohieu==ma)
return i;
return -1;
}
//xuat 1 tai khoan khach hang
void xuat(NH nh)
{ long am;
printf("Nhap so tai khoan: ");
scanf("%ld",&am);
for(int i=0;i<nh.n;i++)
if(nh.tk[i].sohieu==am)
{
xuattaikhoan(nh.tk[i]);
}
}
//hien thi chi tiet danh sach tai khoan
void hienthi(NH nh)
{
for(int i=0;i<nh.n;i++)
xuattaikhoan(nh.tk[i]);
}
// khach hang rut tien
void ruttien(NH &nh,long ma)
{ float tien;
int k=tim(nh,ma) ;
if (k>=0)
{
printf("Nhap so tien muon rut: ");
scanf("%f",&tien);
nh.tk[k].sotien-=tien;
}
else
printf("Tai khoan ko ton tai\n");
}
//xem tien trong tai khoan
void xem(NH nh,long ma)
{ int k=tim(nh,ma);
for( int i=k;i<nh.n;i++)
{
printf("So tien hien co trong tai khoan la: %0.2f\n",nh.tk[i].sotien);
getch();
}
}
//khach hang gui tien
void guitien(NH &nh)
{ float them;
long x;
printf("Nhap so hieu: ");
scanf("%ld",&x);
printf("Nhap so tien muon gui vao: ");
scanf("%f",&them);
for(int i=0;i<nh.n;i++)
if(nh.tk[i].sohieu==x)
{
nh.tk[i].sotien=nh.tk[i].sotien+them;
getch();
}
}
//ngan hang tinh lai suat hang nam
void laisuat(NH &nh)
{
for(int i=0;i<nh.n;i++)
nh.tk[i].sotien+=nh.tk[i].sotien*(nh.tk[i].laisuat/100);
}
//ghi hoa don
int write(NH nh)
{ FILE *f=fopen("C:\DSTK.TXT","wt");
if(f==NULL)
{
printf("Ko mo duoc duoc fie");
return 0;
}
for(int i=0;i<nh.n;i++)
fprintf(f,"Thong tin khach hang:\nSo Hieu: %ld\nHo & Ten khach hang: %s\nSo CMND: %ld\nSo tien: %0.2f\nLai suat: %0.2f\%",nh.tk[i].sohieu,nh.tk[i].ten,nh.tk[i].cmnd,nh.tk[i].sotien,nh.tk[i].laisuat);
fclose(f);
return 0;
}
//doc hoa don
int read(NH nh)
{ FILE *f=fopen("C:\DSTK.TXT","rt");
if(f==NULL)
{
printf("ko mo duoc duoc fie");
return 0;
}
for(int i=0;i<nh.n;i++)
fprintf(f,"\nThong tin khach hang:\nSo Hieu: %ld\nHo & Ten khach hang: %s\nSo CMND: %ld\nSo tien: %0.2f\nLai suat: %0.2f\%",nh.tk[i].sohieu,nh.tk[i].ten,nh.tk[i].cmnd,nh.tk[i].sotien,nh.tk[i].laisuat);
fclose(f);
return 0;
}
void main()
{
clrscr();
NH nh;long ma;int a,d=0;
textcolor(4);
for (int i=0;i<10;i++)
{
printf("\n0: Thoat\n");
printf("\n1: Tao tai khoan moi\n");
printf("\n2: Rut tien trong tai khoan\n");
printf("\n3: Gui them tien vao tai khoan\n");
printf("\n4: Hien thi chi tiet tat ca tai khoan\n");
printf("\n5: Xem chi tiet tai khoan dua vao so tai khoan\n");
printf("\n6: Ngan hang tinh lai suat hang nam\n");
printf("\n7: Xem so tien dang co trong tai khoan\n");
printf("\n8: In hoa don\n");
printf("\n9: Doc hoa don\n");
printf("\nChon muc muon su dung: ");
scanf("%d",&a);
switch(a)
{
case 0:exit(0);
case 1:nhap(nh,d); break;
case 2:printf("Nhap ma: ");scanf("%ld",&ma);ruttien(nh,ma); break;
case 3:guitien(nh); break;
case 4:hienthi(nh); break;
case 6:laisuat(nh); break;
case 5:xuat(nh); break;
case 7:printf("Nhap ma: ");scanf("%ld",&ma);xem(nh,ma); break;
case 8:write(nh); break;
case 9:read(nh); break;
}
}
getch();
}