c - 使用c读取文件中的特定记录

标签 c file

#include<stdio.h>
#include<time.h>
#include<string.h>
#include<stdlib.h>
struct cust
{
   char accno[20],name[30],addr1[60],addr2[60],city[20],acctype[2],phno[20];
   int accbal;
};
void calcTime()  //function to show the current date and time
{
  time_t ts;
  ts=time(NULL);
  printf(ctime(&ts));
}
int main()
{
  char buf[100],a='y',b='y',c='y';
  int amount,total,j,i=0,k=0,l=0;
  struct cust c;
  FILE *p;
  p=fopen("data.DAT","a");
  do
   {
    printf("enter the account number\n");
    scanf("%s", c.accno);
    printf("enter the name and city of customer\n");
    scanf("%s %s",c.name,c.city);
    printf("enter the addr1 and addr2 of the customer\n");
    scanf("%s %s",c.addr1,c.addr2);
    printf("enter the phone no of customer\n");
    scanf("%s",&c.phno);
    printf("enter the account bal of customer\n");
    scanf("%d", &c.accbal);
    printf("enter the account type(s/c)=\n");
    scanf("%s",c.acctype);
      if(strcmp(c.acctype,"s")==0)
        {
          printf("saving account type\n");
        }
      else
         {
           if(strcmp(c.acctype,"c")==0)
            {
              printf("current account type\n");
             }
           else
             {
              printf("enter the currect account type\n");
              scanf("%s,c.acctype");
             }
         }
fseek(p,sizeof(c),SEEK_END);   // set curser point at the end
fwrite(&c,sizeof(c),1,p); //write the details  in file
printf("do you want to type another customer detail(y/n)?=\n");
scanf(" %c",&a);
if(a=='n') 
{
  i=1;
}
}while(!i);
do
{
  printf("enter the account number\n");
  scanf("%s",buf);      //getting the value in buffer
  fseek(p,sizeof(c),SEEK_SET);//set the cursor point at the beginning of file
  while(fread(&c,sizeof(c),1,p))       //read the file
  if(strcmp(buf,c.accno)==0)      //comparing the account number
  {
  do
  {
 printf("enter the choice\n");
 printf("1.view the detail\n");
 printf("2.Deposit the account\n");
 printf("3.view balance amount\n");
 printf("4.amount withdrawl\n");
 scanf("%d",&j);
 switch(j)
 {
  case 1:
    printf("your acc details are follows\n");
    printf("name of the customer=%s\n",c.name);
    printf("city of the customer=%s\n",c.city);
    printf("address1 of customer=%s\n",c.addr1);
    printf("address2 of customer=%s\n",c.addr2);
    printf("balance amount=%d\n",c.accbal);
    printf("phone number of customer=%s\n",&c.phno);
    printf("type of account=%s\n",c.acctype);
    break;
  case 2:
    printf("enter the amount to be deposited=\n");
    scanf("%d",&amount);
    total=c.accbal+amount;
    printf("total value is=%d\n",total);
    c.accbal=total;
    break;
  case 3:
    printf("your balance amount is=%d\n",c.accbal);
    break;
  case 4:
    printf("enter the amount to withdraw=\n");
    scanf("%d",&amount);
    if(amount>c.accbal)
     {
       printf("insufficient balance\n");
     }
    else
     {
       total=c.accbal-amount;
       printf("collect the cash amount\n");
       printf("balance amount=%d\n",total);
       c.accbal=total;
     }
    break;
 default :
    printf("enter the correct choice");
    scanf("%d",j);
    break;
 }
printf("do you want to continue (y/n)=\n");
scanf(" %c",&b);
if(b=='n')
 {
  k=1;
 }while(!k);
 }
printf("Do you need to access another record(y/n)=\n");
scanf(" %c",&c);
if(c=='n')
{ 
l=1;
}while(!l);
}
printf("thanks for using the service\n");
calcTime();
fclose(p);
getchar();
return(0);
}
  1. 我在文件中写入了两条记录的详细信息,但在读取文件时我只得到了第二条记录。 不显示第一条记录。编码中有什么错误?
  2. 请有人告诉我错误是什么。

最佳答案

代码中的一些问题 -

printf("phone number of customer=%s\n",&c.phno);
                                       ^remove this 
scanf("%s,c.acctype");` should be -`scanf("%s",c.acctype);

scanf("%s",&c.phno);
           ^ you don't need this 

你应该停止使用这个 -

fflush(stdin);

它有未定义的行为。

关于c - 使用c读取文件中的特定记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32500400/

相关文章:

c - 如何获取 MagickWand 持有的图像数量?

c - 将用户输入数组 append 到文件 [c]

javascript - Google Sheets - 从硬盘驱动器获取文件名并插入到工作表中 - (模拟 Excel Application.GetOpenFilename)

c - 程序在执行时终止

c - while 循环中 printf() 的奇怪行为

c - 如何在 C 中使用 printf 遍历 unicode 字符并将它们打印在屏幕上?

c - 如何旋转矩阵? C

android - 从 ReSTLet 响应中获取文件

Java 文件列表未使用比较器排序

c - 构建依赖于另一个静态库的静态库