Advertisement
yoyo106

Add function

Dec 15th, 2018
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.81 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. typedef struct
  5. {
  6.     char f[100]; // firstname
  7.     char l[100]; // last name
  8.     char n[100]; //number
  9. }contact; //struct name
  10. void main ()
  11. {
  12.     FILE *fp;
  13.     fp = fopen("phonebook.txt","w");
  14.     printf("How many contacts do you want to add: ");
  15.     int n;
  16.     scanf("%d",&n);
  17.     int i;
  18.     contact arr[n];
  19.     for (i=0;i<n;i++)
  20.     {
  21.         printf("Enter firstname: \n");
  22.         scanf("%s", arr[i].f);
  23.         printf("Enter lastname: \n");
  24.         scanf("%s", arr[i].l);
  25.         printf("Enter number: \n");
  26.         scanf("%s", arr[i].n);
  27.     }
  28.     for (i=0;i<n;i++)
  29.     {
  30.         fprintf(fp,"%s",arr[i].f);
  31.         fprintf(fp,",%s",arr[i].l);
  32.         fprintf(fp,",%s.",arr[i].n);
  33.         fprintf(fp,"\n");
  34.     }
  35.     fclose(fp);
  36.  
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement