Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <ctype.h>
- typedef struct only_time{
- int hour;
- int minute;
- int second;
- }only_time_t;
- typedef struct product{
- char * code_product;
- char * name;
- char * code_piece;
- only_time_t enter;
- only_time_t exit;
- }product_t;
- typedef struct product_list{
- product_t product;
- struct product_list *next;
- }product_list_t;
- FILE * open_file(char *c);
- product_t * read_line(FILE * fp);
- only_time_t timestring_to_time(char * timestring);
- void print_product(product_t * product);
- int is_number(char *param);
- int main(void)
- {
- FILE * fp;
- fp = open_file("test.txt");
- while(1){
- if(feof(fp))
- break;
- product_t * product = read_line(fp);
- print_product(product);
- }
- fclose(fp);
- return(0);
- }
- // returns file pointer, input string of filename
- FILE * open_file(char *c){
- FILE * fp;
- fp = fopen(c, "r");
- return(fp);
- }
- // reads an entire file.
- product_t * read_line(FILE * fp){
- char * line = NULL;
- size_t len = 0;
- product_t * temp;
- int i = 0;
- temp = (product_t *) malloc(sizeof(product_t));
- temp->code_product = (char *) malloc(sizeof(char) * 4);
- temp->name = (char *) malloc(sizeof(char) * 60);
- temp->code_piece = (char *) malloc(sizeof(char) * 4);
- //read a line from the file
- getline(&line, &len, fp);
- //handle line info info
- char *tokens[80];
- tokens[0] = strtok(line," ,.");
- while (tokens[i] != NULL) {
- i++;
- tokens[i] = strtok(NULL," ,.");
- }
- temp->code_product = tokens[0];
- temp->name = tokens[1];
- temp->code_piece = tokens[2];
- temp->enter = timestring_to_time(tokens[3]);
- temp->exit = timestring_to_time(tokens[4]);
- return(temp);
- }
- only_time_t timestring_to_time(char * timestring){
- only_time_t time;
- int i = 0;
- char *tokens[5];
- //init values:
- time.hour = 0;
- time.minute = 0;
- time.second = 0;
- tokens[0] = strtok(timestring,":;");
- while (tokens[i] != NULL) {
- i++;
- tokens[i] = strtok(NULL," :;");
- }
- time.hour = atoi(tokens[0]);
- time.minute = atoi(tokens[1]);
- time.second = atoi(tokens[2]);
- return(time);
- }
- void print_product(product_t * product){
- printf("product_t code_product: %s \n", product->code_product);
- printf("product_t name: %s \n", product->name);
- printf("product_t code_piece: %s \n", product->code_piece);
- printf("product_t enter: %d:%d:%d \n", product->enter.hour,product->enter.minute,product->enter.second);
- printf("product_t exit: %d:%d:%d \n", product->exit.hour,product->exit.minute,product->exit.second);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement