gpt4 book ai didi

c - 无法读取 C 中的 .dat 文件

转载 作者:行者123 更新时间:2023-11-30 18:11:07 24 4
gpt4 key购买 nike

我创建了一个程序,它从用户那里获取帐户信息并将其存储到文件accounts.dat中。由于该程序现在是这样,看起来一切正常,直到我关闭它并重新打开以测试它,它不会读取我已经输入的任何信息。情况 6 是应该从 accounts.dat 读取的地方。每次我使用accounts.dat时,它似乎都充满了信息。

/* HEADER FILES */
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

/* STRUCT TO HOLD ACCOUNT INFO */
struct bank
{
char first[25];
char middle[25];
char last[25];
long int aNumber;
float aBalance;
};

/* MAIN START*/
int main()
{
int choice; /* SWITCH-STATEMENT CASE */
int i; /* FOR-LOOP COUNTER */
int count; /* KEEPS TRACK OF TOTAL # OF ACCOUNTS */
long int aNumber; /* TEMPORARILY HOLDS ACCOUNT # FOR SEARCH IN FILE */
float amount; /* TEMPORARILY HOLDS A DOLLAR AMOUNT FOR VARIOUS REASONS */
FILE *fp; /* SPECIFIC FILE DECLARATION FOR OUR BANK */
struct bank account[50]; /* ARRAY DECLARATION FOR STRUCT */

/* DO/WHILE START */
do
{
/* OPTION MENU */
printf("\n\nWelcome to yor bank access.\n"
"Please follow the prompts below.\n\n"
"\t0. Exit\n"
"\t1. Deposit\n"
"\t2. Withdrawal\n"
"\t3. Add account\n"
"\t4. Remove Account\n"
"\t5. Balance Inquiry\n"
"\t6. View Account\n"
"\nEnter your choice: ");
scanf("%d",&choice);
printf("\n");

/* SWITCH START */
switch(choice)
{

/* CASE 0: EXIT PROGRAM */
case 0:
exit(1);
break;

/* CASE 1: DEPOSIT */
case 1:
fopen("accounts.dat", "r+");
fseek(fp,0,SEEK_SET);
printf("please enter account number: ");
scanf("%ld",&aNumber);
printf("Amount to be deposited: $");
scanf("%f", &amount);
for(i=0; i<count; i++)
{
if(account[i].aNumber == aNumber)
{
account[i].aBalance = account[i].aBalance + amount;
fseek(fp,i * sizeof(struct bank),SEEK_SET);
fwrite(account+i,sizeof(struct bank),1, fp);
break;
}
}
if(i==count)
printf("Account number does not exits\n");
break;

/* CASE:2 WITHDRAWAL */
case 2:
fopen("accounts.dat", "r+");
fseek(fp,0,SEEK_SET);
printf("Please enter account number: ");
scanf("%ld",&aNumber);
printf("Enter the amount to be withdrawn: $");
scanf("%f",&amount);
for(i=0; i<count; i++)
{
if(account[i].aNumber==aNumber)
{
if(account[i].aBalance<amount)
{
printf("Insufficient balance\n");
break;
}
account[i].aBalance=account[i].aBalance-amount;
fseek(fp,i*sizeof(struct bank),SEEK_SET);
fwrite(account+i,sizeof(struct bank),1, fp);
break;
}
}
if(i==count)
printf("Account number does not exits\n");
break;

/* CASE 3: ADD ACCOUNT */
case 3:
fp = fopen("accounts.dat","a+");
printf("Enter a 6-digit account number: ");
scanf("%ld",&aNumber);
for(i=0; i<count; i++)
{
if(account[i].aNumber==aNumber)
{
printf("Account already exist\n");
break;
}
}
if(i==count)
{
account[i].aNumber=aNumber;

printf("Enter the First Name: ");
scanf("%s",account[i].first);

printf("Enter the Middle Initial: ");
scanf("%s",account[i].middle);

printf("Enter the Last Name: ");
scanf("%s",account[i].last);

printf("Enter the Amount: $");
scanf("%f",&account[i].aBalance);
fseek(fp,0,SEEK_END);
fwrite(account+i,sizeof(struct bank),1, fp);
count++;
}
break;

/* CASE 4: DELETE ACCOUNT */
case 4:
fopen("accounts.dat", "a+");
fseek(fp,0,SEEK_SET);
printf("Please enter account number: ");
scanf("%ld",&aNumber);
for(i=0; i<count; i++)
{
if(account[i].aNumber==aNumber)
break;
}
if(i==count)
printf("Account number does not exits\n");
else
{
while(i<count)
{
strcpy(account[i].first,account[i+1].first);
strcpy(account[i].middle,account[i+1].middle);
strcpy(account[i].last,account[i+1].last);
account[i].aNumber=account[i+1].aNumber;
account[i].aBalance=account[i+1].aBalance;
}
count--;
fp=fopen("accounts.dat", "wb");
for(i=0; i<count; i++)
fwrite(account+i,sizeof(struct bank),1, fp);
fclose(fp);
/* REOPEN FILE */
fopen("accounts.dat", "r+");
}
break;

/* CASE 5: BALANCE INQUIRY*/
case 5:
fopen("accounts.dat", "r");
printf("Please enter account number: ");
scanf("%ld",&aNumber);
for(i=0; i<count; i++)
{
if(account[i].aNumber==aNumber)
{
printf("First Name: %s\n",account[i].first);
printf("Middle Initial: %s\n",account[i].middle);
printf("Last Name: %s\n",account[i].last);
printf("Account Number: %ld\n",account[i].aNumber);
printf("Balance Amount: $%.2f\n",account[i].aBalance);
break;
}
}
if(i==count)
printf("Account number does not exits\n");
break;

/* CASE 6: VIEW ACCOUNTS*/
case 6:
fopen("accounts.dat", "r");
for(i=0; i<count; i++)
{
fread(&account[i], sizeof(account), 1, fp);
printf("Entry %1d\n", i+1);
printf("First Name: %s\n",account[i].first);
printf("Middle Initial: %s\n",account[i].middle);
printf("Last Name: %s\n",account[i].last);
printf("Account Number: %ld\n",account[i].aNumber);
printf("Account Balance: $%.2f\n",account[i].aBalance);
}
} /* END SWITCH-STATEMENT */
} /* END 'DO' */
while(choice!=0);
return 0;
}

最佳答案

当您使用情况 6 启动程序时,count 未初始化,导致 for 循环出现意外行为。

关于c - 无法读取 C 中的 .dat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51623952/

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