int main(){ FILE *f_text , *f_pattern; int found, text_ln=line_num("text.txt"); f_text = fopen("text.txt", "r"); fseek(f_text, 0, SEEK_END); int text_file_size = ftell(f_text); fclose(f_text); f_text = fopen("text.txt", "r"); char *text; text = (char*)malloc (text_file_size+1); text[text_file_size]='\0'; f_pattern = fopen ( "pattern.txt" , "r"); fseek (f_pattern,0, SEEK_END); int pattern_file_size = ftell (f_pattern); fclose(f_pattern); f_pattern = fopen ( "pattern.txt" , "r"); char *pattern; pattern = (char*)malloc (pattern_file_size+1); pattern[pattern_file_size]=0; if(f_pattern!= NULL){ fgets(pattern, sizeof(char)+pattern_file_size, f_pattern); } int i, j, k; if(f_text != NULL && f_pattern!=NULL){ for (;text_ln--;){ fgets(text, sizeof(char)+text_file_size,f_text); for (i=0;i<text_file_size-pattern_file_size;i++){ if (pattern[0]==text[i]) { k=i; for (j=0;j<pattern_file_size;j++){ if (pattern[j]==text[k]) { k++; if (j+1==pattern_file_size) { printf("pattern found at index %d \n", i); found=1; } } else break; } } } } } fclose(f_text); fclose(f_pattern); if(found==1) printf ("The pattern was found.\n"); else printf("The pattern wasn't found.\n"); free (text); free (pattern); return (0); }
struct course * get_courses_file(char * file_name, struct node * nodes) { //list of the courses attribute struct course * course_list; FILE * courses_file; char * mode = "r"; //number of courses in the file num_of_courses = line_num(file_name); //create memory for the number of courses in the file course_list = (struct course*) malloc(num_of_courses * sizeof (struct course)); //open the file courses_file = fopen(file_name, mode); int i; //loop over the file for (i = 0; i < num_of_courses; i++) { struct course new_course; char course_name; int num_nodes_in_course; int node_count = 0; struct node * course_nodes; if (courses_file != NULL) { fscanf(courses_file, "%c %d", &course_name, &num_nodes_in_course); new_course.course_name = course_name; new_course.num_of_courses = num_of_courses; new_course.num_nodes_in_course = num_nodes_in_course; //create the memory for to store all the nodes in the course course_nodes = (struct node *) malloc(num_nodes_in_course * sizeof (struct node)); //loop through the line getting the nodes for (node_count = 0; node_count < num_nodes_in_course; node_count++) { int current_node; fscanf(courses_file, "%d", ¤t_node); //get node from nodes list course_nodes[node_count] = nodes[current_node - 1]; } //set the nodes list in the course new_course.nodes_list = course_nodes; } fscanf(courses_file, "\n"); //assign the course in the course list course_list[i] = new_course; } fclose(courses_file); return course_list; }