I have a runtime error that i cant figure out (SOLVED)

Started by
13 comments, last by Kylotan 18 years ago
the runtime error I get is Run-Time Check Failure #2 - Stack around the variable 'chChount' was corrupted. I cant see anything wrong with what I am doing here can you? here is my code

#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
	ifstream infile;
	string buffer;
	char chbuf;

	long chCount[256]= {0};
	//memset(chCount, 0, 256 * sizeof(int));

	cout<<"please entere a filename to analyze: ";
	getline(cin, buffer);
	infile.open(buffer.c_str(), ios::in);

	if(infile.is_open())
	{
	while(!infile.eof())
	{

		chbuf = infile.get();
	
		chCount[chbuf]++;
	}

	for(int i =0;i<256;i++)
	{
		if(chCount != 0)
		{
			cout<<char(i)<<": "<<chCount<<endl;
		}
	}

	infile.close();
	}
	return 0;
}


[Edited by - donjonson on March 25, 2006 2:02:39 PM]
These tears..leave scars...as they run down my face.
Advertisement
Does istream::get() return EOF on end of file? If so, the constant for an integer EOF will be an invalid character, either > 265 or < 0. Either way, you're writing outside your array bounds. istream::get() returns an int, not a char, so this is a possibility.
On what line do you get this error exactly?
Hey bud,

At first guess i would say that it is because you have a char array of length 256 but are only setting the first element, upon initialisation.

Dave
Quote:Original post by Dave
Hey bud,

At first guess i would say that it is because you have a char array of length 256 but are only setting the first element, upon initialisation.

Dave


That wouldn't cause an overflow. It would cause incorrect results though..
while(!infile.eof()){	chbuf = infile.get();		chCount[chbuf]++;}//I think the error is there. Try this...int i=0;while(!infile.eof())		chCount[i++] = infile.get();


edit:

I fixed it for you =)

#include <iostream>#include <string>#include <fstream>using namespace std;int main(){	cout<<"please entere a filename to analyze: ";	string buffer;		getline(cin, buffer);	ifstream infile(buffer.c_str());		if(infile.is_open() == false)		return 1;		char chCount[256];	int i = 0;	while(!infile.eof())						chCount[i++] = infile.get();		chCount = '\0';	i=0;	while(chCount && i < 256)		cout << chCount[i++];		cin.ignore();}


[Edited by - simon10k on March 25, 2006 12:29:49 PM]
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
You're reading into a char, which you shouldn't do. You should read into an int. This is because you're not guaranteed to get just a char. In fact, you may receive 'EOF' which is a value that may not fit into a char.

Secondly, the range of char is -128 to 127. You may therefore read in a value that doesn't fit in your array. In fact it would end up fitting before your array.

So what you should do, is read into an int, check it's not EOF, then cast it to an unsigned char before using it as an array index.
try changing "char chbuf" to "unsigned char chbuf"
Simon10k: That isn't what he was trying to do. In fact, that code is even more broken as it does NO bounds checking and will almost certainly overflow and probably crash. This is a perfect example of how NOT to read data from a file.
Whatever... This is a messy example to start with. I just noticed tho, that your right! he was trying to count the letters. *feels stupid =)*
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003

This topic is closed to new replies.

Advertisement