Thread: Storing Words from a text file

  1. #16
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks a lot, that helps a lot. I'm on my last stat, I have to find the most commonly occuring word(s). Heres my function-

    Code:
    void commonWord(double count[], int array_length, words array[])
    {
    	int commonCount[Num];
    	int max;
    	max = count[0];
    	int j = 0;
    	int i = 0;
    	int k = 0;
    	
    	for(i = 1; i<array_length; i++)
    	{
    		if(count[i] > max)
    			max = count[i];
    		else if( count[i] == max)
    			{
    				commonCount[j] = i;
    				j++;
    			}	
    	}
    	
    	cout<< "The most commonly occuring words are: "<< endl;
    	
    	for( k = 0; k<array_length; k++)
    		cout<< array[commonCount[k]].word<< endl;
    }
    I dont get a compile-time error. But when I run the program, I get a message telling me the .exe file stopped working.

  2. #17
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    I'm now trying to write my stats to a text file, but its only writing two of the stats, and those stats come from the same function.

    Heres the code -


    Code:
    #include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int const wordLength = 21;
    int const Num = 100;
    int const fileSize = 255;
    
    struct words
    {
    	char word[wordLength];
    	int count;	
    };
    
    
    	
    void storeFile(char[], char [], words []);
    void displayFile(char[], char[], words[]);
    int wordSearch(char word[], int array_size, words []);
    void sortSetup(char[], words [], int);
    int sort (words [], int, int);
    void averageLength(char[], words[], int);
    void Occurrence(char[], words[], int);
    void commonWord(char[], double[], int, words []);
    void averageOccurrence(char[], words[], int);
    
    int main ()
    {
    	
    	words array[Num];
    	char fileName[fileSize];
    	char out_file_name[fileSize]; 
    	
    	cout << "Please enter the name of the file you wish to open: "<< endl;
    	cin.getline(fileName,fileSize);
    	
    	if (!cin.good() ) { 
    		cout << "Error reading cin..." << endl ; 
    		return -1 ; 
    	} 
    	
    	cout<<"Please enter the name of the  file you wish to send the data too" << endl;
    	cin.getline(out_file_name,fileSize);
    	
    	displayFile(out_file_name, fileName, array);
    	storeFile(out_file_name, fileName, array);
    	
    	cin.ignore();
    }
    
    void storeFile (char out_file_name[], char fileName[], words array[] )
    {	
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int i = 0;
    	ifstream inFile;
    	char line [Num];
    	int array_size = 0 ;  
    	inFile.open(fileName);
    
    	while (inFile.getline(line,Num))
    	{   
    		array_size = wordSearch(line, array_size, array);
    
    		i++;    
    	}   
    	
    	inFile.close(); 
    
    	outFile<< "The number of unique words are: "<< array_size << endl;
    	outFile<< "total number of words are: " << i << endl;
    	outFile<< endl;
    
    }
    
     int wordSearch( char line[], int array_size, words array[])
    {
    	int i = array_size ;  
    	
    	
    	while (i && array_size > 0)   
    	{
    		if (strcmp(line, array[i-1].word) == 0)  
    		{ 
    			array[i-1].count++;   
    			return  array_size ;   
    		}
    		i-- ;  
    	}
        
    	strcpy(array[array_size].word, line) ; 
    	array[array_size].count = 1 ; 
    	return array_size+1;   
    }
    
    void displayFile (char out_file_name[], char fileName[], words array[] )
    {
    	int i = 0;
    	char line [Num];
    	
    	ifstream inFile;
    	inFile.open(fileName);
    	
    	
    	while (inFile.getline(line,Num))
    	{   
    	
    		strncpy(array[i].word, line, wordLength);
    		i++;
    	}   
    	 
        sortSetup(out_file_name, array, i);
        averageLength(out_file_name, array, i);
    	Occurrence(out_file_name, array, i);
    	averageOccurrence(out_file_name, array, i);
    	
    	inFile.close();
    }
    
    void sortSetup (char out_file_name[], words array [], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    		
    	char temp[wordLength];
        int position;
        int i = array_length;
    	
        for (int loop = 0; loop < array_length - 1; loop++)
        {
            position = sort (array, loop, array_length - 1);
            if (position != loop)
            {
                strcpy(temp, array[position].word);
                strcpy(array[position].word, array[loop].word);
                strcpy(array[loop].word, temp);
            }
        }
    	
    	outFile << "The words in alphabetical order are:"<< endl;
    
    	for (int j = 0; j< array_length; j++)
    	{
    		if(strcmp(array[j].word,array[j-1].word)!=0)
    			outFile << array[j].word << endl;
    	}	
    	
    	outFile << endl;
    }
    
    
    int sort (words array[], int start, int stop)
    {
        int n;
    	int loc = start;
        for (int pos = start + 1; pos <= stop; pos++)
        {    
    		n = (strcmp(array[pos].word, array[loc].word));
    		
    		if (n < 0)
                loc = pos;
        }
    	return loc;
    }
    
    void averageLength(char out_file_name[], words array[], int i)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	double average = 0;
    	for(int j = 0; j<i; j++)
    		average = average + strlen(array[j].word);
    	
    	average = average/i;
    	
    	outFile << "The average length of the words are: " << average <<endl;
    	outFile << endl;
    }	
    
    void Occurrence(char out_file_name[], words array[], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int n;
    	char cmp_array[wordLength];
    	double count[Num];
    	
    	for( int i= 0; i< array_length; i++)
    	{	
    		strcpy(cmp_array, array[i].word);
    		count[i] = 0;
    		for (int j=0; j<array_length; j++)
    		{
    			n = (strcmp(array[j].word, cmp_array));
    			if(n == 0)
    			count[i]++;
    		}
    	}
    	
    	outFile<<"The unique words and the number of times they appear in the text file appears asthe following:"<< endl; 
    	outFile<<"word/times it appears:" << endl;
    	outFile<< endl;
    	for (int k = 0; k< array_length; k++)
    	{
    		if(strcmp(array[k].word,array[k-1].word)!=0)
    			outFile <<array[k].word << " / " << count[k] <<  endl;
    	}
    	
    	outFile<<endl;
    	commonWord(out_file_name, count, array_length, array);
    }
    
    
    void commonWord(char out_file_name[], double count[], int array_length, words array[])
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int count_max;
    	count_max = count[0];
    	int j = 0;
    	int i = 0;
    	
    	
    	for(i = 1; i<array_length; i++)
    	{
    		if(count[i] > count_max)
    			count_max = count[i];
    	}
    
    	outFile<< "The word(s) that occur the most are: "<< endl;
    	
    	for( j = 0;  j<array_length; j++)
    	{
    		if(strcmp(array[j].word,array[j-1].word)!=0)
    		{
    		  if(count[j] == count_max)
    		
    			outFile << array[j].word<< endl;
    		}	
    	}		
    
    	outFile<< endl;
    }		
    
    void averageOccurrence(char out_file_name[], words array[], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int n;
    	char cmp_array[wordLength];
    	double count[Num];
    	
    	for( int i= 0; i< array_length; i++)
    	{	
    		strcpy(cmp_array, array[i].word);
    		count[i] = 0;
    		for (int j=0; j<array_length; j++)
    		{
    			n = (strcmp(array[j].word, cmp_array));
    			if(n == 0)
    			count[i]++;
    		}
    	}
    	
    	outFile<<"The average occurence of a word appears as the following:" << endl; outFile <<"word/average appearence:" << endl;
    	outFile<< endl;
    	for (int k = 0; k< array_length; k++)
    	{
    		if(strcmp(array[k].word,array[k-1].word)!=0)
    			outFile <<array[k].word << " / " << count[k]/array_length <<  endl;
    	}
    	
    	outFile<<endl;
    }#include <iostream>
    #include <fstream>
    #include <cstdlib>
    #include <string>
    #include <iomanip>
    using namespace std;
    
    int const wordLength = 21;
    int const Num = 100;
    int const fileSize = 255;
    
    struct words
    {
    	char word[wordLength];
    	int count;	
    };
    
    
    	
    void storeFile(char[], char [], words []);
    void displayFile(char[], char[], words[]);
    int wordSearch(char word[], int array_size, words []);
    void sortSetup(char[], words [], int);
    int sort (words [], int, int);
    void averageLength(char[], words[], int);
    void Occurrence(char[], words[], int);
    void commonWord(char[], double[], int, words []);
    void averageOccurrence(char[], words[], int);
    
    int main ()
    {
    	
    	words array[Num];
    	char fileName[fileSize];
    	char out_file_name[fileSize]; 
    	
    	cout << "Please enter the name of the file you wish to open: "<< endl;
    	cin.getline(fileName,fileSize);
    	
    	if (!cin.good() ) { 
    		cout << "Error reading cin..." << endl ; 
    		return -1 ; 
    	} 
    	
    	cout<<"Please enter the name of the  file you wish to send the data too" << endl;
    	cin.getline(out_file_name,fileSize);
    	
    	displayFile(out_file_name, fileName, array);
    	storeFile(out_file_name, fileName, array);
    	
    	cin.ignore();
    }
    
    void storeFile (char out_file_name[], char fileName[], words array[] )
    {	
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int i = 0;
    	ifstream inFile;
    	char line [Num];
    	int array_size = 0 ;  
    	inFile.open(fileName);
    
    	while (inFile.getline(line,Num))
    	{   
    		array_size = wordSearch(line, array_size, array);
    
    		i++;    
    	}   
    	
    	inFile.close(); 
    
    	outFile<< "The number of unique words are: "<< array_size << endl;
    	outFile<< "total number of words are: " << i << endl;
    	outFile<< endl;
    
    }
    
     int wordSearch( char line[], int array_size, words array[])
    {
    	int i = array_size ;  
    	
    	
    	while (i && array_size > 0)   
    	{
    		if (strcmp(line, array[i-1].word) == 0)  
    		{ 
    			array[i-1].count++;   
    			return  array_size ;   
    		}
    		i-- ;  
    	}
        
    	strcpy(array[array_size].word, line) ; 
    	array[array_size].count = 1 ; 
    	return array_size+1;   
    }
    
    void displayFile (char out_file_name[], char fileName[], words array[] )
    {
    	int i = 0;
    	char line [Num];
    	
    	ifstream inFile;
    	inFile.open(fileName);
    	
    	
    	while (inFile.getline(line,Num))
    	{   
    	
    		strncpy(array[i].word, line, wordLength);
    		i++;
    	}   
    	 
        sortSetup(out_file_name, array, i);
        averageLength(out_file_name, array, i);
    	Occurrence(out_file_name, array, i);
    	averageOccurrence(out_file_name, array, i);
    	
    	inFile.close();
    }
    
    void sortSetup (char out_file_name[], words array [], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    		
    	char temp[wordLength];
        int position;
        int i = array_length;
    	
        for (int loop = 0; loop < array_length - 1; loop++)
        {
            position = sort (array, loop, array_length - 1);
            if (position != loop)
            {
                strcpy(temp, array[position].word);
                strcpy(array[position].word, array[loop].word);
                strcpy(array[loop].word, temp);
            }
        }
    	
    	outFile << "The words in alphabetical order are:"<< endl;
    
    	for (int j = 0; j< array_length; j++)
    	{
    		if(strcmp(array[j].word,array[j-1].word)!=0)
    			outFile << array[j].word << endl;
    	}	
    	
    	outFile << endl;
    }
    
    
    int sort (words array[], int start, int stop)
    {
        int n;
    	int loc = start;
        for (int pos = start + 1; pos <= stop; pos++)
        {    
    		n = (strcmp(array[pos].word, array[loc].word));
    		
    		if (n < 0)
                loc = pos;
        }
    	return loc;
    }
    
    void averageLength(char out_file_name[], words array[], int i)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	double average = 0;
    	for(int j = 0; j<i; j++)
    		average = average + strlen(array[j].word);
    	
    	average = average/i;
    	
    	outFile << "The average length of the words are: " << average <<endl;
    	outFile << endl;
    }	
    
    void Occurrence(char out_file_name[], words array[], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int n;
    	char cmp_array[wordLength];
    	double count[Num];
    	
    	for( int i= 0; i< array_length; i++)
    	{	
    		strcpy(cmp_array, array[i].word);
    		count[i] = 0;
    		for (int j=0; j<array_length; j++)
    		{
    			n = (strcmp(array[j].word, cmp_array));
    			if(n == 0)
    			count[i]++;
    		}
    	}
    	
    	outFile<<"The unique words and the number of times they appear in the text file appears asthe following:"<< endl; 
    	outFile<<"word/times it appears:" << endl;
    	outFile<< endl;
    	for (int k = 0; k< array_length; k++)
    	{
    		if(strcmp(array[k].word,array[k-1].word)!=0)
    			outFile <<array[k].word << " / " << count[k] <<  endl;
    	}
    	
    	outFile<<endl;
    	commonWord(out_file_name, count, array_length, array);
    }
    
    
    void commonWord(char out_file_name[], double count[], int array_length, words array[])
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int count_max;
    	count_max = count[0];
    	int j = 0;
    	int i = 0;
    	
    	
    	for(i = 1; i<array_length; i++)
    	{
    		if(count[i] > count_max)
    			count_max = count[i];
    	}
    
    	outFile<< "The word(s) that occur the most are: "<< endl;
    	
    	for( j = 0;  j<array_length; j++)
    	{
    		if(strcmp(array[j].word,array[j-1].word)!=0)
    		{
    		  if(count[j] == count_max)
    		
    			outFile << array[j].word<< endl;
    		}	
    	}		
    
    	outFile<< endl;
    }		
    
    void averageOccurrence(char out_file_name[], words array[], int array_length)
    {
    	ofstream outFile;
    	outFile.open(out_file_name);
    	
    	int n;
    	char cmp_array[wordLength];
    	double count[Num];
    	
    	for( int i= 0; i< array_length; i++)
    	{	
    		strcpy(cmp_array, array[i].word);
    		count[i] = 0;
    		for (int j=0; j<array_length; j++)
    		{
    			n = (strcmp(array[j].word, cmp_array));
    			if(n == 0)
    			count[i]++;
    		}
    	}
    	
    	outFile<<"The average occurence of a word appears as the following:" << endl; outFile <<"word/average appearence:" << endl;
    	outFile<< endl;
    	for (int k = 0; k< array_length; k++)
    	{
    		if(strcmp(array[k].word,array[k-1].word)!=0)
    			outFile <<array[k].word << " / " << count[k]/array_length <<  endl;
    	}
    	
    	outFile<<endl;
    }
    The "storeFile" function is the only one that prints to the file.
    BTW I know this code is unorganized and not the best way to do it, but this project is due tomorrow (12-10) so I'm just trying to turn in what I have.

  3. #18
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    They each write out to the file just fine. But of course, since each function starts by erasing the file before writing to it....

  4. #19
    Registered User
    Join Date
    Oct 2008
    Posts
    25
    Thanks for the quick reply. I started messing with "outFile.put(out_file_name);" but that didnt work. Out professor told us only to read the first two chapters on "I/O File Streams and Data Files". However it wasn't intill the 5th chapter where it starts explaining file stream as function arguments. So I wonder why he didnt make that chapter assign reading. Well anyways this thread is solved.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. storing text file contents into a string
    By m.mixon in forum C Programming
    Replies: 4
    Last Post: 07-20-2006, 11:52 AM
  2. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  3. Problem with malloc() and sorting words from text file
    By goron350 in forum C Programming
    Replies: 11
    Last Post: 11-30-2004, 10:01 AM
  4. Removing text between /* */ in a file
    By 0rion in forum C Programming
    Replies: 2
    Last Post: 04-05-2004, 08:54 AM
  5. Storing words from a file to an array
    By SIKCAR in forum C Programming
    Replies: 10
    Last Post: 09-09-2002, 06:47 AM