Advertisement
Guest User

Untitled

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