Fread()和fwrite()函数

时间:2015-01-13 23:11:51

标签: c fread

#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作为选项时

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

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

1 个答案:

答案 0 :(得分:0)

我删除了特定于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

他的钱多于你的钱。