gpt4 book ai didi

c - fread() 和 fwrite() 函数

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

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <conio.h>

void delay (int milliseconds); // for delay function
void menu (); //for choosing a menu
void addacc(); // for adding account
void view (); // for viewing existing list
struct date
{
int date, month, year; //struct for date
};

struct customer
{
char name[40],acctype[10];
int accno, age;
double phone;
float amount;
struct date dob; //calling other struct inside struct
struct date deposit;
struct date withdraw;
} add; //struct variable

void addacc()
{


FILE *fp;
fp=fopen ("cus.txt", "a+");
textcolor (1);
printf ("\n\t\t\t\t");
cprintf ("ADD RECORD");
printf("\n\n\n");
printf ("Enter today's date(date/month/year) \n");
scanf ("%d/%d/%d", &add.deposit.date, &add.deposit.month,&add.deposit.year);
printf ("Enter account number\n");
scanf ("%d", &add.accno);
printf ("Enter customer's name\n");
scanf ("%s", add.name);
printf ("Enter customer's age\n");
scanf ("%d", &add.age); printf ("Enter customer's phone num\n");
scanf ("%f",&add.phone);
printf ("Enter the account type(in words): \n\t 1:Current\n\t 2:Saving\n\t 3:Fixed\n");
scanf ("%s",&add.acctype);
textcolor (2);
cprintf ("Almost done! Just enter the amount you want to deposit: ");
scanf ("%f",&add.amount);

fwrite (&add,sizeof(add),1,fp);
fclose (fp);

}

void view ()
{
FILE *view;
int test=0;
system ("cls");
textcolor (3);
printf ("\n\t\t\t\t");
cprintf ("Customer's List");
printf ("\n\n\n");
textcolor(4);
cprintf ("\tCustomer's Name:");
cprintf ("\tAccount Number:");
cprintf ("\tCustomer's Phone No:");
view=fopen("cus.txt", "r");

while(fread(&add, sizeof(add),1,view)!=0)
{
printf ("%s", add.name);
printf ("%d", add.accno);
printf ("%f", add.phone);
test++;

}
fclose (view);
if (test==0)
{
printf ("NO RECORDS FOUND!");
}
}

void menu ()
{
int n;
printf ("Enter your choice 1, 2\n");
scanf ("%d", &n);

switch (n)

{
case 1:
addacc();
break;
case 2:
view ();
break;
}
}

void main (void)
{
system ("cls");

menu ();
}

输出:当您选择 1 作为选项时

添加记录输入今天的日期输入客户姓名ETC输出:当您选择选项 2 查看现有客户列表时屏幕空白

所以我想知道我的 fread 语法还是 fwrite 语法错误?为什么屏幕上没有显示我刚刚输入的条目?我正在使用 fread 函数将结构读入文件,然后我想在屏幕上打印条目。

最佳答案

我删除了特定于 Windows 的代码并进行了一些清理。代码认真地检查每个 scanf() 是否返回了正确的值;懒惰地,如果不存在,它就会退出。它还检查对 fopen() 的调用是否有效。

一些 scanf() 调用是不可靠的。字符串名称前面不需要 &,但需要 %lf 来读取 double(电话号码 -有趣的数据类型选择)。我确保换行符出现在输出中。

尽管存在长名称问题,但打印效果要好一些。

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

void menu(void); // for choosing a menu
void addacc(void); // for adding account
void view(void); // for viewing existing list

struct date
{
int date, month, year; // struct for date
};

struct customer
{
char name[40], acctype[10];
int accno, age;
double phone;
float amount;
struct date dob; // calling other struct inside struct
struct date deposit;
struct date withdraw;
} add; // struct variable

void addacc(void)
{
FILE *fp = fopen("cus.txt", "a+");
if (fp == NULL)
exit(1);
printf("ADD RECORD\n");
printf("Enter today's date(date/month/year) \n");
if (scanf("%d/%d/%d", &add.deposit.date, &add.deposit.month, &add.deposit.year) != 3)
exit(1);
printf("Enter account number\n");
if (scanf("%d", &add.accno) != 1)
exit(1);
printf("Enter customer's name\n");
if (scanf("%s", add.name) != 1)
exit(1);
printf("Enter customer's age\n");
if (scanf("%d", &add.age) != 1)
exit(1);
printf("Enter customer's phone num\n");
if (scanf("%lf", &add.phone) != 1)
exit(1);
printf("Enter the account type(in words): \n\t 1:Current\n\t 2:Saving\n\t 3:Fixed\n");
if (scanf("%s", add.acctype) != 1)
exit(1);
printf("Almost done! Just enter the amount you want to deposit: ");
if (scanf("%f", &add.amount) != 1)
exit(1);

fwrite(&add, sizeof(add), 1, fp);
fclose(fp);
}

void view(void)
{
FILE *view;
int test = 0;
printf("Customer's List\n");
printf("\tCustomer's Name:");
printf("\tAccount Number:");
printf("\tCustomer's Phone No:\n");
view = fopen("cus.txt", "r");
if (view == NULL)
exit(1);

while (fread(&add, sizeof(add), 1, view) != 0)
{
printf("\t%16s", add.name);
printf("\t%15d", add.accno);
printf("\t%20.0f", add.phone);
putchar('\n');
test++;
}
fclose(view);
if (test == 0)
{
printf("NO RECORDS FOUND!");
}
}

void menu(void)
{
int n;
printf("Enter your choice 1, 2\n");
if (scanf("%d", &n) != 1)
exit(1);

switch (n)
{
case 1:
addacc();
break;
case 2:
view();
break;
}
}

int main(void)
{
menu();
return 0;
}

示例输出:

Enter your choice 1, 2
2
Customer's List
Customer's Name: Account Number: Customer's Phone No:
BushraYousuf 12345678 112345987621
PresidentBarackObama 987654321 2021199920

他藏的钱比你多。

关于c - fread() 和 fwrite() 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933226/

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