Advertisement
Guest User

Untitled

a guest
Jun 6th, 2018
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.65 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5.  
  6. typedef struct only_time{
  7.     int hour;
  8.     int minute;
  9.     int second;
  10. }only_time_t;
  11.  
  12. typedef struct product{
  13.     char * code_product;
  14.     char * name;
  15.     char * code_piece;
  16.     only_time_t enter;
  17.     only_time_t exit;
  18. }product_t;
  19.  
  20. typedef struct product_list{
  21.     product_t product;
  22.     struct product_list *next;
  23. }product_list_t;
  24.  
  25. FILE * open_file(char *c);
  26. product_t * read_line(FILE * fp);
  27. only_time_t timestring_to_time(char * timestring);
  28. void print_product(product_t * product);
  29. int is_number(char *param);
  30.  
  31. int main(void)
  32. {
  33.     FILE * fp;
  34.     fp = open_file("test.txt");
  35.     while(1){
  36.         if(feof(fp))
  37.             break;
  38.         product_t * product = read_line(fp);
  39.         print_product(product);
  40.     }
  41.     fclose(fp);
  42.     return(0);
  43. }
  44.  
  45. // returns file pointer, input string of filename
  46. FILE * open_file(char *c){
  47.     FILE * fp;
  48.  
  49.     fp = fopen(c, "r");
  50.     return(fp);
  51. }
  52.  
  53. // reads an entire file.
  54. product_t * read_line(FILE * fp){
  55.     char * line = NULL;
  56.     size_t len = 0;
  57.     product_t * temp;
  58.     int i = 0;
  59.    
  60.     temp = (product_t *) malloc(sizeof(product_t));
  61.    
  62.     temp->code_product = (char *) malloc(sizeof(char) * 4);
  63.     temp->name = (char *) malloc(sizeof(char) * 60);
  64.     temp->code_piece = (char *) malloc(sizeof(char) * 4);
  65.    
  66.     //read a line from the file
  67.     getline(&line, &len, fp);
  68.    
  69.     //handle line info info
  70.     char *tokens[80];
  71.    
  72.     tokens[0] = strtok(line," ,.");
  73.    
  74.     while (tokens[i] != NULL) {
  75.         i++;
  76.         tokens[i] = strtok(NULL," ,.");                            
  77.     }
  78.    
  79.     temp->code_product = tokens[0];
  80.     temp->name = tokens[1];
  81.     temp->code_piece = tokens[2];
  82.     temp->enter = timestring_to_time(tokens[3]);
  83.     temp->exit = timestring_to_time(tokens[4]);
  84.    
  85.     return(temp);
  86. }
  87.  
  88. only_time_t timestring_to_time(char * timestring){
  89.     only_time_t time;
  90.     int i = 0;
  91.     char *tokens[5];
  92.    
  93.     //init values:
  94.     time.hour = 0;
  95.     time.minute = 0;
  96.     time.second = 0;
  97.    
  98.     tokens[0] = strtok(timestring,":;");
  99.    
  100.     while (tokens[i] != NULL) {
  101.         i++;
  102.         tokens[i] = strtok(NULL," :;");                        
  103.     }
  104.    
  105.     time.hour = atoi(tokens[0]);
  106.     time.minute = atoi(tokens[1]);
  107.     time.second = atoi(tokens[2]);
  108.    
  109.     return(time);
  110.    
  111. }
  112.  
  113. void print_product(product_t * product){
  114.     printf("product_t code_product: %s \n", product->code_product);
  115.     printf("product_t name: %s \n", product->name);
  116.     printf("product_t code_piece: %s \n", product->code_piece);
  117.     printf("product_t enter: %d:%d:%d \n", product->enter.hour,product->enter.minute,product->enter.second);
  118.     printf("product_t exit: %d:%d:%d \n", product->exit.hour,product->exit.minute,product->exit.second);
  119. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement