Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. //
  2. // Created by Blu-Ray on 12/8/2019.
  3. //
  4. #include<stdio.h>
  5. #include<conio.h>
  6. #include<string.h>
  7. #include<stdlib.h>
  8. #include "functions.h"
  9.  
  10. void getNumOfLines(FILE * f){
  11. char c;
  12. for (c = getc(f); c != EOF; c = getc(f)) {
  13. if (c == '\n')
  14. count = count + 1;
  15. }
  16. count++;
  17. }
  18. DateOfBirth *BirthdayConstructor(char* s)
  19. {
  20. DateOfBirth *birthday = malloc(sizeof(DateOfBirth));
  21. int i = 0;
  22. char *p = strtok(s,"/'\'- ");
  23. while(p)
  24. {
  25. if (i == 0) {
  26. strcpy(birthday->day, p);
  27. }
  28. else if (i == 1) {
  29. strcpy(birthday->month, p);
  30. }
  31. else if (i == 2) {
  32. strcpy(birthday->year, p);
  33. }
  34. p = strtok(NULL,"/'\'- ");
  35. i++;
  36. }
  37. return birthday;
  38. }
  39.  
  40. Contact *saveToStruct(char* str){
  41. Contact *res = malloc(sizeof(Contact));
  42. int flag = 0;
  43. char *token = strtok(str, ",");
  44. DateOfBirth *bd;
  45. while( token != NULL )
  46. {
  47. if (flag == 0)
  48. strcpy(res->lastName, token);
  49. else if (flag == 1)
  50. strcpy(res->firstName, token);
  51. else if(flag == 2) {
  52. bd = BirthdayConstructor(token);
  53. res->dateOfBirth = *bd;
  54. }
  55. else if(flag == 3)
  56. strcpy(res->stName,token);
  57. else if(flag == 4)
  58. strcpy(res->email,token);
  59. else if(flag == 5)
  60. strcpy(res->phoneNum,token);
  61. flag++;
  62. token = strtok( NULL, "," );
  63. }
  64. return res;
  65. }
  66. void load(){
  67. system("cls");
  68. Contact *c;
  69. char fileName[] = "hai.txt";
  70. // printf("Enter file name: ");
  71. // scanf("%s",fileName);
  72. FILE *f;
  73. f = fopen(fileName,"r");
  74. if(f==NULL) {printf("File doesn't exist\n"); getch();
  75. return;}
  76. getNumOfLines(f);
  77. printf("%s\n",f->_base);
  78. char* token = strtok(f->_base,"\n");
  79. printf("%s\n",token);
  80. Contact *p ;
  81. p=saveToStruct(token);
  82. arrCon[0] = *p;
  83. printf("%s\n",arrCon->phoneNum);
  84. fclose(f);
  85. printf("Press any key to continue ..");
  86. getch();
  87.  
  88. }
  89. void saveFile(){
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement