Advertisement
Guest User

Untitled

a guest
Oct 21st, 2018
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.84 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <ctype.h>
  4. #include <conio.h>
  5. #include <string.h>
  6.  
  7. //define struct
  8. typedef struct stud{
  9.     char name[20];
  10.     int age;
  11.     int nrm;
  12.     char adrs[30];
  13. } L_stud;
  14.  
  15. //other global variables
  16. L_stud *list;
  17.  
  18.  
  19. //function prototypes
  20. void read();
  21. char *check_name(char name[]);
  22. int check_num(char num[]);
  23.  
  24.  
  25.  
  26.  
  27.  
  28. int main()
  29. {
  30.  
  31.     int choice = -1;
  32.  
  33.     printf("\n");
  34.     printf("Make a choice from below:\n 1.Read \n 2. \n 3. \n 4. \n 5.Exit\n ");
  35.     scanf("%d", &choice);
  36.  
  37.     while(choice)
  38. {
  39.         switch(choice){
  40.         case 1:
  41.             read();
  42.             break;
  43.         case 2:
  44.  
  45.             break;
  46.         case 3:
  47.  
  48.             break;
  49.         case 4:
  50.  
  51.             break;
  52.         case 5:
  53.             exit(0);
  54.             break;
  55.         default:
  56.             printf("Error! Wrong choice.\n");
  57.     }
  58.     printf("Make a choice from below:\n 1. \n 2. \n 3. \n 4. \n 5.Exit\n ");
  59.     scanf("%d", &choice);
  60. }
  61.  
  62.  
  63.  
  64. }
  65.  
  66.  
  67. void read(){
  68.     FILE *fp = fopen("data.in", "r");
  69.  
  70.     list = (L_stud*) malloc(5 * sizeof(L_stud));
  71.  
  72.     int i = 0;
  73.     char single_line[50];
  74.     char sep[2] = ",";
  75.     char *token;
  76.  
  77.     while (!feof(fp)&& fgets(single_line, 50, fp)!= NULL)
  78.     {
  79.  
  80.         fgets(single_line, 50, fp);
  81.  
  82.         printf("Am citit %s\n",single_line);
  83.  
  84.         token = strtok(single_line, sep);
  85.         strcpy(list[i].name, check_name(token));
  86.         printf("%s\n", list[i].name);
  87.  
  88.         token = strtok(NULL, sep);
  89.         list[i].age = check_num(token);
  90.         printf("Age: %d\n",list[i].age);
  91.  
  92.         token = strtok(NULL, sep);
  93.         list[i].nrm = check_num(token);
  94.         printf("Nrm: %d\n",list[i].nrm);
  95.  
  96.         token = strtok(NULL, sep);
  97.         token[strlen(token)] = '\0';
  98.         printf("%s",token);
  99.  
  100.         strcpy(list[i].adrs, token);
  101.         printf("Adresa %s\n",list[i].adrs);
  102.  
  103.         i++;
  104.  
  105.     }
  106.  
  107.  
  108.     printf("\nThe file was succesfully read!\n");
  109.  
  110.     fclose(fp);
  111. }
  112.  
  113. char *check_name(char name[])
  114. {
  115.     int len = strlen(name);
  116.     char *new_name = (char*) malloc(len * sizeof(char));
  117.     new_name[0] = '\0';
  118.  
  119.     int i = 0;
  120.     for( i = 0; i < len; i++)
  121.     {
  122.         if (isalpha(name[i]))
  123.         {
  124.             int length = strlen(new_name);
  125.  
  126.             new_name[length] = name[i];
  127.             new_name[length + 1] = '\0';
  128.         }
  129.  
  130.     }
  131.  
  132.     return new_name;
  133. }
  134.  
  135. int check_num(char num[])
  136. {
  137.     int val;
  138.     int len = strlen(num);
  139.     char *new_name = (char*) malloc(len * sizeof(char));
  140.     new_name[0] = '\0';
  141.  
  142.     int i = 0;
  143.     for( i = 0; i < len; i++)
  144.     {
  145.         if (isdigit(num[i]))
  146.         {
  147.             int length = strlen(new_name);
  148.  
  149.             new_name[length] = num[i];
  150.             new_name[length + 1] = '\0';
  151.         }
  152.  
  153.     }
  154.     val = atoi(new_name);
  155.     return val;
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement