void function2(){ ofstream out("131250072_2.txt"); fstream temp_file("temp.txt", ios::out | ios::in | ios::trunc);//must add ios::trunc to create the file if it is not exist if (back.is_open() && out.is_open() && temp_file.is_open()) { int count_of_word = 0; int diff_word = 0; back.seekg(0, ios::end); int size = back.tellg(); // cout << size << endl; char *s = new char[size] ;//save all file in it back.seekg(0, ios::beg);//start from beginning, for ios::end while (back.getline(s, size)){//TODO why I have to write a endl to make back readable??? cout << "one time" << endl; int beg_of_word = 0; int len = 0; while (split(&s[beg_of_word], &len)) {//find the first appearence of word count_of_word = 1; diff_word++; const int dul = len; const int duw = beg_of_word; char s1[MAX_WORD] = {0}; char s2[MAX_WORD] = {0}; strncpy(s1, empty_word, 100); strncpy(s1, &s[duw], dul); for (beg_of_word+=len ;split(&s[beg_of_word], &len); beg_of_word+=len) {//scan the rest part, so to count the times of it strncpy(s2, empty_word, 100); strncpy(s2, &s[beg_of_word], len); if (my_strcmp(s1, s2) == 0){//compare strings ignoring the space count_of_word ++;//count of same word memset((void*)&s[beg_of_word], ' ', len);//set the same word in array blank } } beg_of_word = duw;//find the beginning of last word beg_of_word += dul;//to next word //write to a file extract_word(s1); temp_file << count_of_word << endl << s1 << endl; } //make tokens Token *tokens = new Token[diff_word]; int in_token = 0; char tem_w[MAX_WORD] = {0}; bool num_word = true;//true means it is a num, false means it's a word temp_file.seekp(0, ios::beg);//TODO why I have to add it to make it readable??? write to it will change the get position? while(temp_file.getline(tem_w, MAX_WORD)){//getline will clear the last word's wreakage if (num_word) { tokens[in_token].times = atoi(tem_w); num_word = false; } else { strncpy(tokens[in_token++].str, tem_w, MAX_WORD); num_word = true; } } //sort sort_token(tokens, 0, diff_word); //print for (int i = 0; i < diff_word; ++i) { out << tokens[i].str << " " << tokens[i].times << endl; } delete[] tokens; } } else { cout << out.is_open() << endl; cout << temp_file.is_open() << endl; cout << back.is_open() << endl; cout << "can't open file" << endl; } }