When I output the return value of the InitialSeedFinder function seen below (aka value stored in the seed variable), I get some random ascii chars that corrupt the expected string value. This only happens if the buffer is more 2 chars (ie. it works for when the order variable is less than 3).
This error is being introduced in the while loop seen in the code below...
Can someone explain WHY this is happening? does it have something to do with how the read() function works?
string InitialSeedFinder(int order, string fileName){ string seed; ifstream inputStream; Map<string, int> frequencyMap; inputStream.open(fileName.c_str()); int offset = 0; inputStream.clear(); char* buffer = new char [order]; //get all k char sequence while (inputStream.get() != EOF) { inputStream.seekg(offset); inputStream.read(buffer, order); string key(buffer); if (frequencyMap.containsKey(key)) { frequencyMap[key] = frequencyMap[key] + 1; } else { frequencyMap.put(key, 1); } offset++; } inputStream.close(); //go through and find the most frequent key int greatestFrequency = 0; int frequency = 0; foreach(string key in frequencyMap) { frequency = frequencyMap[key]; if (frequency > greatestFrequency) { greatestFrequency = frequencyMap[key]; seed = key; } } return seed; }