fork download
  1. #include<stdio.h>
  2.  
  3. struct Contact{
  4. int No;
  5. char name [20];
  6. char phoneNumber[30];
  7. char Email_address[45];
  8. };
  9.  
  10. void Emptydata(int max,FILE *f)
  11. {
  12. unsigned int i;
  13.  
  14. struct Contact EmptyContact = {0,"","",""};
  15.  
  16. for(i=1;i<=100;++i)
  17. {
  18. fwrite(&EmptyContact, sizeof(struct Contact),1,f);
  19. }
  20. }
  21. int main(void)
  22. {
  23. int user;
  24. int counter=0;
  25. int numOfRecord=0,iterator=0;
  26. FILE *f;
  27.  
  28. f=fopen("Contact.dat","wb+");
  29. Emptydata(50,f);
  30.  
  31. struct Contact c[2]={{1,"Ariel","0981-154-203","Tong@gmail.com"},{2,"Chubao","0910-653-758","Chu@gmail.com"}};
  32. fseek(f,0,SEEK_SET);
  33. fwrite(c,sizeof(struct Contact),sizeof(c)/sizeof(struct Contact),f);
  34. fclose(f);
  35.  
  36.  
  37. printf("Enter the number of option you want(1 for read the existing contact, 2 for add new contact.)\n");
  38. scanf("%d",&user);
  39.  
  40. if(user==1)
  41. {
  42. f=fopen("Contact.dat","rb");
  43. fseek(f,0,SEEK_END);
  44.  
  45. numOfRecord=ftell(f)/sizeof(struct Contact);
  46. rewind(f);
  47.  
  48. struct Contact c[numOfRecord];
  49. fread(c,sizeof(struct Contact),numOfRecord,f);
  50.  
  51. for(iterator=0;iterator<numOfRecord;++iterator)
  52. {
  53. printf("%d%10s%20s%20s\n",c[iterator].No,c[iterator].name,c[iterator].phoneNumber,c[iterator].Email_address);
  54. }
  55. }
  56. if(user==2)
  57. {
  58. f=fopen("Contact.dat","r+b");
  59.  
  60. struct Contact New ={0,"","",""};
  61.  
  62. printf("Enter your name,phone number,and email address.\n");
  63. fscanf(stdin,"%10s%20s%20s",New.name,New.phoneNumber,New.Email_address);
  64.  
  65. counter++;
  66. fseek(f,(counter+2)*sizeof(struct Contact),SEEK_END);
  67.  
  68. numOfRecord=ftell(f)/sizeof(struct Contact);
  69. rewind(f);
  70.  
  71. fwrite(&New,sizeof(struct Contact),numOfRecord-2,f);
  72.  
  73. struct Contact c[numOfRecord];
  74. fread(c,sizeof(struct Contact),numOfRecord,f);
  75.  
  76. for(iterator=0;iterator<numOfRecord;++iterator)
  77. {
  78. printf("%s%20s%20s\n",c[iterator].name,c[iterator].phoneNumber,c[iterator].Email_address);
  79. }
  80. }
  81. fclose(f);
  82. }
Runtime error #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
Standard output is empty