Fread()和fwrite()函数 #包括 #包括 #包括 #包括 无效延迟(整数毫秒);//关于延迟函数 无效菜单()//用于选择菜单 void addacc();//用于添加帐户 无效视图();//用于查看现有列表 结构日期 { int date,month,year;//日期的结构 }; 结构客户 { 字符名[40],acctype[10]; 国际帐户,年龄; 双电话; 浮动金额; struct date dob;//调用struct中的其他struct 结构日期存款; 结构日期撤销; }添加//结构变量 void addacc() { 文件*fp; fp=fopen(“cus.txt”,“a+”); textcolor(1); printf(“\n\t\t\t\t”); cprintf(“添加记录”); printf(“\n\n\n”); printf(“输入今天的日期(日期/月/年)\n”); scanf(“%d/%d/%d”、&add.deposit.date、&add.deposit.month、&add.deposit.year); printf(“输入帐号”); scanf(“%d”和add.accno); printf(“输入客户名称”); scanf(“%s”,add.name); printf(“输入客户的年龄”); scanf(“%d”和&add.age);printf(“输入客户的电话号码”); scanf(“%f”和add.phone); printf(“输入帐户类型(大写):\n\t 1:当前\n\t 2:保存\n\t 3:固定\n”); scanf(“%s”、&add.acctype); textcolor(2); cprintf(“差不多完成了!只需输入您要存入的金额:”); scanf(“%f”、&add.amount); fwrite(&add,sizeof(add),1,fp); fclose(fp); } 无效视图() { 文件*视图; int检验=0; 系统(“cls”); textcolor(3); printf(“\n\t\t\t\t”); cprintf(“客户名单”); printf(“\n\n\n”); textcolor(4); cprintf(“\t客户名称:”); cprintf(“\t计数编号:”); cprintf(“\t客户电话号码:”); 视图=fopen(“cus.txt”、“r”); while(fread(&add,sizeof(add),1,view)!=0) { printf(“%s”,add.name); printf(“%d”,add.accno); printf(“%f”,add.phone); 测试++; } fclose(视图); 如果(测试==0) { printf(“未找到任何记录!”); } } 无效菜单() { int n; printf(“输入您的选择1,2\n”); scanf(“%d”和“&n”); 开关(n) { 案例1: addacc(); 打破 案例2: 视图(); 打破 } } 真空总管(真空) { 系统(“cls”); 菜单(); }

Fread()和fwrite()函数 #包括 #包括 #包括 #包括 无效延迟(整数毫秒);//关于延迟函数 无效菜单()//用于选择菜单 void addacc();//用于添加帐户 无效视图();//用于查看现有列表 结构日期 { int date,month,year;//日期的结构 }; 结构客户 { 字符名[40],acctype[10]; 国际帐户,年龄; 双电话; 浮动金额; struct date dob;//调用struct中的其他struct 结构日期存款; 结构日期撤销; }添加//结构变量 void addacc() { 文件*fp; fp=fopen(“cus.txt”,“a+”); textcolor(1); printf(“\n\t\t\t\t”); cprintf(“添加记录”); printf(“\n\n\n”); printf(“输入今天的日期(日期/月/年)\n”); scanf(“%d/%d/%d”、&add.deposit.date、&add.deposit.month、&add.deposit.year); printf(“输入帐号”); scanf(“%d”和add.accno); printf(“输入客户名称”); scanf(“%s”,add.name); printf(“输入客户的年龄”); scanf(“%d”和&add.age);printf(“输入客户的电话号码”); scanf(“%f”和add.phone); printf(“输入帐户类型(大写):\n\t 1:当前\n\t 2:保存\n\t 3:固定\n”); scanf(“%s”、&add.acctype); textcolor(2); cprintf(“差不多完成了!只需输入您要存入的金额:”); scanf(“%f”、&add.amount); fwrite(&add,sizeof(add),1,fp); fclose(fp); } 无效视图() { 文件*视图; int检验=0; 系统(“cls”); textcolor(3); printf(“\n\t\t\t\t”); cprintf(“客户名单”); printf(“\n\n\n”); textcolor(4); cprintf(“\t客户名称:”); cprintf(“\t计数编号:”); cprintf(“\t客户电话号码:”); 视图=fopen(“cus.txt”、“r”); while(fread(&add,sizeof(add),1,view)!=0) { printf(“%s”,add.name); printf(“%d”,add.accno); printf(“%f”,add.phone); 测试++; } fclose(视图); 如果(测试==0) { printf(“未找到任何记录!”); } } 无效菜单() { int n; printf(“输入您的选择1,2\n”); scanf(“%d”和“&n”); 开关(n) { 案例1: addacc(); 打破 案例2: 视图(); 打破 } } 真空总管(真空) { 系统(“cls”); 菜单(); },c,fread,C,Fread,输出:选择1作为选项时 添加记录 输入今天的日期 输入客户的姓名 等 输出:选择选项2查看现有客户列表时 屏幕空白 所以我想知道我的fread语法是错误的还是fwrite语法?为什么屏幕上没有显示我刚才输入的条目?我正在使用fread函数将结构读入文件,然后我想在屏幕上打印条目 我删除了特定于Windows的代码,并清理了一些东西。代码认真地检查每个scanf()是否返回了正确的值;懒洋洋地,如果没有,它就退出。它还检查对fopen()的调用是否有效 一些scanf()调用是不可靠的。您不需要在

输出:选择1作为选项时

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


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

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

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

虽然存在名称过长的问题,但打印效果要好一些

#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 ();
}

他存的钱比你的多。

你能简化你的代码并提供输入和预期输出的示例吗?你有什么问题吗?欢迎来到堆栈溢出。请尽快阅读这一页。这主要是一个问答网站,但您还没有提出任何问题。你已经发布了一些代码。显然(从问题标题来看),您关心的是
fread()
fwrite()
函数,但您没有向我们展示您得到了什么、想要得到什么、正在使用的输入等。请查看如何创建MCVE()。代码是fread和fwrite函数的实现。这家伙和我们分享了代码,以便在我们的项目中使用它们。非常感谢。bushra@MustafaChelik:有趣的想法,但实现似乎缺少关键功能,这表明你的假设是错误的。非常感谢你,相信我,从过去两天开始,我一直停留在这一点上,它没有显示我搜索了大约20个网站的条目,我无法识别错误。昨天我终于在这里做了一个交代,我真的很感激。
#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