fread() 和 fwrite() 函数

Fread() and fwrite () function

#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(phone 数字 — 有趣的数据类型选择) .我确保换行符出现在输出中。

印刷效果好一些,但名称过长会出现一些问题。

#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

他藏的钱比你多