#include #include #include FILE *fptr; struct __attribute__((packed)) { unsigned int w0 : 1; unsigned int w1 : 1; unsigned int w2 : 1; unsigned int w3 : 1; unsigned int w4 : 1; unsigned int f0 : 1; } bitfield; typedef struct lightarray { int8_t **lights; int rows_of_lights; int columns_of_lights; //int number·of·houses; //int x·coord; //int y·coord; //int x·coord·bot; //int y·coord·bot; //int x_coord_origin; //int y_coord_origin; //int seq·no; } __attribute__((packed)) Lightarray; //initial array allocation Lightarray *initlightarray(){ Lightarray* f = (Lightarray*)malloc(sizeof(Lightarray)); if (f == NULL) return NULL; f->rows_of_lights = 1000; f->columns_of_lights = 1000; f->lights = (int8_t**)malloc(f->rows_of_lights * sizeof(int8_t*));//allocating the rows if (f->lights == NULL) { free(f); return NULL; //handling if there isn't enough memory to allocate or allocation fails } f->lights[0] = (int8_t*)malloc(f->columns_of_lights * sizeof(int8_t)); if (f->lights[0] == NULL){ free(f->lights); free(f); return NULL; } return f; } int main(){ char compare_1[35]; char compare_2[35]; int pos_1; int pos_2; fptr = fopen("input", "r"); if(fptr == NULL) { printf("Not able to open the file."); return 1; } Lightarray* my_light_array = initlightarray(0); if (my_light_array == NULL){ fprintf(stderr, "ERROR:initialisation of struct failed\n"); return 1; } char buffer[266]; while (fgets(buffer, sizeof(buffer), fptr)) { bitfield.w0 = 0; bitfield.w1 = 0; bitfield.w2 = 0; bitfield.w3 = 0; bitfield.w4 = 0; bitfield.f0 = 0; sprintf(compare_1,"%.4s", buffer); sprintf(compare_2,"%.3s", buffer + 5); printf("%s\n\n",buffer); printf("%s\n",compare_2); if (strcmp(compare_1,"turn")){ bitfield.w0 = 1; printf("turn found\n"); }else{ bitfield.w1 =1; printf("toggle found\n"); } if (bitfield.w0 == 1 && strcmp(compare_2,"on")){ bitfield.w2 = 1; } else if (bitfield.w0 == 1 && strcmp(compare_2,"off")){ bitfield.w3 = 1; } //scrape section pos_1 = strcspn(buffer,"123456789"); pos_2 = strcspn(buffer,","); //pos_1 to pos_2 is the length of the first number } }