Sep 10, 2011 at 11:51pm UTC
Hi, I am trying to read a text file through a char array called originalWord and then process the words in the file into wordArray because I can't directly read into wordArray. The problem is that I keep getting a segmentation fault instead of the words from the file. Here is my code:
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cctype>
#include <cstring>
#include <cstdlib>
using std::exit;
using std::setw;
using std::ifstream;
using std::cin;
using std::cout;
using std::endl;
using std::cerr;
const int WORD_LENGTH = 20;
const int ARRAY_SIZE = 500;
void removePunctuation(char [], char []);
int searchForWord(char[][WORD_LENGTH + 1], int, char []);
void sortWords(char[][WORD_LENGTH + 1], int [], int);
void printWords(char[][WORD_LENGTH + 1], int [], int);
int main()
{
ifstream inFile;
char wordArray[ARRAY_SIZE][WORD_LENGTH + 1];
int wordCountArray[ARRAY_SIZE] = {0};
int numWords = 0;
char originalWord[WORD_LENGTH + 1];
char cleanedWord[WORD_LENGTH + 1];
int index = 0;
inFile.open("words.txt");
if (inFile.fail())
{
cerr << "File did not open..";
exit(-1);
}
while (inFile >> originalWord)
{
removePunctuation(originalWord, cleanedWord);
if (strlen(cleanedWord) == 0) //skip if word is all punctuation
break;
for (int i = 0; cleanedWord[i] != '\0'; i++) //convert all chars to uppercase
{
cleanedWord[i] = toupper(cleanedWord[i]);
}
index = searchForWord(wordArray, numWords, cleanedWord);
if (index >= ARRAY_SIZE)
cerr << "ERROR wordArray is full..";
else if (index == numWords)
{
strcpy(wordArray[index], cleanedWord);
wordCountArray[index] = 1;
numWords = numWords + 1;
}
else
wordCountArray[index] = wordCountArray[index] + 1;
}
inFile.close();
sortWords(wordArray, wordCountArray, numWords);
printWords(wordArray, wordCountArray, numWords);
return 0;
}
/****************************************************************
*/
void removePunctuation(char originalWord[], char cleanedWord[])
{
int start = 0, newStart = 0;
int end = strlen(originalWord - 1);
while (originalWord[start] != isalnum(originalWord[start]) && start <= end)
start = start + 1;
while (originalWord[start] != isalnum(originalWord[start]) && start >= end)
end = end - 1;
while (start <= end)
{
strcpy(cleanedWord[newStart], originalWord[start]);
start = start + 1;
newStart = newStart + 1;
}
cleanedWord[newStart] = '\0';
}
int searchForWord(char wordArray[][WORD_LENGTH + 1], int numWords, char cleanedWord[])
{
int i;
for (i = 0; i < numWords; i++)
{
if (strcmp(cleanedWord, wordArray[i]))
break;
}
if (i == ARRAY_SIZE)
return -1;
else
return i;
}
void sortWords(char wordArray[][WORD_LENGTH + 1], int wordCountArray[], int numWords)
{
int i, j, min;
int countTemp;
char wordTemp[WORD_LENGTH + 1];
for (i = 0; i < (numWords - 1); i++)
{
min = i;
for (j = i+1; j < numWords; j++)
{
if (strcmp(wordArray[j], wordArray[min]))
min = j;
}
countTemp = wordCountArray[min];
wordCountArray[min] = wordCountArray[i];
wordCountArray[i] = countTemp;
strcpy(wordTemp, wordArray[min]);
strcpy(wordArray[min], wordArray[i]);
strcpy(wordArray[i], wordTemp);
}
}
void printWords(char wordArray[][WORD_LENGTH + 1], int wordCountArray[], int numWords)
{
cout << "Word Counts for words.txt" << endl << endl;
for (int i = 0; wordArray[i] != '\0'; i++)
{
if (i % 3 == 0)
cout << endl;
cout << wordArray[i];
for (int j = 0; wordCountArray[j] != '\0'; j++)
{
if (j % 3 == 0)
cout << endl;
cout << wordCountArray[j];
}
}
cout << endl << "Number of distinct words: " << numWords << endl;
}
Any help would be appreciated.
Thanks.