Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <ctype.h>
- #include <conio.h>
- #include <string.h>
- //define struct
- typedef struct stud{
- char name[20];
- int age;
- int nrm;
- char adrs[30];
- } L_stud;
- //other global variables
- L_stud *list;
- //function prototypes
- void read();
- char *check_name(char name[]);
- int check_num(char num[]);
- int main()
- {
- int choice = -1;
- printf("\n");
- printf("Make a choice from below:\n 1.Read \n 2. \n 3. \n 4. \n 5.Exit\n ");
- scanf("%d", &choice);
- while(choice)
- {
- switch(choice){
- case 1:
- read();
- break;
- case 2:
- break;
- case 3:
- break;
- case 4:
- break;
- case 5:
- exit(0);
- break;
- default:
- printf("Error! Wrong choice.\n");
- }
- printf("Make a choice from below:\n 1. \n 2. \n 3. \n 4. \n 5.Exit\n ");
- scanf("%d", &choice);
- }
- }
- void read(){
- FILE *fp = fopen("data.in", "r");
- list = (L_stud*) malloc(5 * sizeof(L_stud));
- int i = 0;
- char single_line[50];
- char sep[2] = ",";
- char *token;
- while (!feof(fp)&& fgets(single_line, 50, fp)!= NULL)
- {
- fgets(single_line, 50, fp);
- printf("Am citit %s\n",single_line);
- token = strtok(single_line, sep);
- strcpy(list[i].name, check_name(token));
- printf("%s\n", list[i].name);
- token = strtok(NULL, sep);
- list[i].age = check_num(token);
- printf("Age: %d\n",list[i].age);
- token = strtok(NULL, sep);
- list[i].nrm = check_num(token);
- printf("Nrm: %d\n",list[i].nrm);
- token = strtok(NULL, sep);
- token[strlen(token)] = '\0';
- printf("%s",token);
- strcpy(list[i].adrs, token);
- printf("Adresa %s\n",list[i].adrs);
- i++;
- }
- printf("\nThe file was succesfully read!\n");
- fclose(fp);
- }
- char *check_name(char name[])
- {
- int len = strlen(name);
- char *new_name = (char*) malloc(len * sizeof(char));
- new_name[0] = '\0';
- int i = 0;
- for( i = 0; i < len; i++)
- {
- if (isalpha(name[i]))
- {
- int length = strlen(new_name);
- new_name[length] = name[i];
- new_name[length + 1] = '\0';
- }
- }
- return new_name;
- }
- int check_num(char num[])
- {
- int val;
- int len = strlen(num);
- char *new_name = (char*) malloc(len * sizeof(char));
- new_name[0] = '\0';
- int i = 0;
- for( i = 0; i < len; i++)
- {
- if (isdigit(num[i]))
- {
- int length = strlen(new_name);
- new_name[length] = num[i];
- new_name[length + 1] = '\0';
- }
- }
- val = atoi(new_name);
- return val;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement