#include "stdafx.h" #include <conio.h> #include <stdio.h> #include <string> #include <iostream> using namespace std; struct word { char value[25]; int number; }; int main() { char str[200], str1[200]; int m = 0, k = 0; FILE *file, *file1, *file2; if( ( file = fopen( "text.txt", "r" ) ) == NULL ) { puts( "Error" ); exit( 1 ); } while( fgets( str, 200, file ) == NULL ); fclose( file ); char *ptr = strtok( str, ",.-!?" ); while( ptr != NULL ) { cout << ptr; if( ( file1 = fopen( "new.txt", "a" ) ) == NULL ) { puts( "Error" ); exit( 1 ); } fputs( ptr, file1 ); fclose( file1 ); ptr = strtok( NULL, ",.-!?" ); } cout << endl; cout << "ZZZ" << endl; if( ( file2 = fopen( "new.txt", "r" ) ) == NULL ) { puts( "Error" ); exit( 1 ); } fgets( str1, 200, file2 ); for( int i = 0; i < 200; i++ ) { if( str1[i] == ' ' ) { k++; } } cout << "AAA" << endl; cout << "The number of the words = " << k + 1 << endl; fclose( file2 ); word *d = new word[k]; if( ( file2 = fopen( "new.txt", "r" ) ) == NULL ) { puts( "Error" ); exit( 1 ); } while( fscanf( file2, "%s", d[m].value ) != EOF ) { m++; } fclose( file2 ); for( int i = 0; i < k + 1; i++ ) { cout << d[i].value << endl; } cout << "ZZZ" << endl; for( int i = 0; i < k + 1; i++ ) { d[i].number = 1; } for( int i = 0; i < k + 1; i++ ) { for( int j = 0; j < k + 1; j++ ) { if( i != j ) { if( _stricmp( d[i].value, d[j].value ) == 0 ) { d[i].number++; } } } } for( int i = 0; i < k + 1; i++ ) { if( d[i].number ) { cout << "The number of word which are the same with " << d[i].value << " " << d[i].number << endl; } } _getch(); return 0; } 

This program counts the number of occurrences of each word in the text. At the end she displays the word and the amount. The problem is that each word is displayed, but it is true, but you need one instance to write to the new file and the word. I wanted to rewrite the word and its number of entries into the new structure one by one, but could not. Help.

  • Why do you have three pointers to the file being declared? :) - Vlad from Moscow
  • As for this cycle, while (fgets (str, 200, file) == NULL); then at best the variable str will be the last line of the file. - Vlad from Moscow
  • one
    All errors from the previous question migrated 1: 1 to this one. Why do you ignore the advice to use the debugger and look at them with your own eyes? - PinkTux
  • the number of words counts fine - vasul2538
  • I just can not write the result in a new structure - vasul2538

0