M
machikelxol
I'm having a strange error when I try reading from a file. here is the
code:
buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the
4th iteration.
This works fine until the fourth iteration.
breaks on this line: buffString = new char[numLineItems];
The contents of the text file it is reading:
1,1,1,1,1,1,1,1,1,5,6,7,8,9
2,2,2,2,2,2,2,2,2
3,3,3,3,3,3,3,3,3
4,4,4,4,4,4,4,4,4
5,5,5,5,5,5,5,5,5
6,6,6,6,6,6,6,6,6
Any ideas on how to fix this or what the problem may be? Need to get
the entire file read in and not error out on the fourth iteration.
(errors out on line with 5's on it)
Code is below:
void DrawWorld::LoadParseFile()
{
//string to store data in LoadParseFile(ifstream*) function
theFile = new ifstream("Maps\\1.txt");
char currentCharacter = '\0';
char *tokenBuffer = "";
int arraySize = 0;
int index = 0, innerindex = 0;
if (!theFile)
{
exit(1); // terminate with error
}
//deterimine size of file
while(!theFile->eof())
{
currentCharacter = theFile->get();
//increment our arraySize if we have a \n
if (currentCharacter == '\n')
{
arraySize++;
}
}
//there is typically one more
//row in a file than carriage returns
arraySize++;
//clear eof state
theFile->clear();
theFile->seekg(0);
/*initialize our arrays
the tiles*/
tiles = new int[arraySize];
//whether the tile is enterable
enterable = new int[arraySize];
/*whether the character is displayed
behind or in front of the tile*/
tileBehind = new int[arraySize];
//for storing animation on/off 1 = yes 0 = no
tileAnimationOnOff = new int[arraySize];
//whether the animation repeats or not 1 = yes 0 = no
tileAnimationLoop = new int[arraySize];
//for storing the time to wait in between animation switches
tileAnimationFrequency = new int[arraySize];
//for storing the animations
tileAnimations = new int*[arraySize];
while (!theFile->eof())
{
char *buffString;
//get a line out of mapFile and assign
//it to buffstring
char lineCounter[1];
int numLineItems = 0;
int currentLocation = theFile->tellg();
lineCounter[0] = theFile->get();
//determine number of characters in current line
while(lineCounter[0] != '\n')
{
lineCounter[0] = theFile->get();
numLineItems++;
}
int test = theFile->tellg();
//redimension our array to the right size for the line
buffString = new char[numLineItems];
//drop back the same number of characters
//that we just read
theFile->seekg(currentLocation);
//get the exact number of characters we need
theFile->getline(buffString, 100);
//first item is the tile number
tiles[index] = atoi(strtok(buffString, ","));
//next is enterable
enterable[index] = atoi(strtok(NULL, ","));
//tilebehind
tileBehind[index] = atoi(strtok(NULL, ","));
//tileAnimationOnOff
tileAnimationOnOff[index] = atoi(strtok(NULL, ","));
//tileRepeat
tileAnimationLoop[index] = atoi(strtok(NULL, ","));
//animation speed
tileAnimationFrequency[index] = atoi(strtok(NULL, ","));
//deterimine size of the inner array
int innerArraySize = 0, i = 0;
//grab the remaining characters in this line
tokenBuffer = strtok(NULL, "");
//begin the while with the first character of tokenBuffer
//TODO: only do this if tokenBuffer has a value
currentCharacter = tokenBuffer[0];
//while through the character array
while(currentCharacter != '\0')
{
//we are counting the commas, this will
//give us an accurate measurement of the
//elements we will have in our inner array
if (currentCharacter == ',')
{
innerArraySize++;
}
i++;
//select the next item in the character array
currentCharacter = tokenBuffer;
}
//we'll have 1 more element than we have commas
innerArraySize++;
//allocate memory for our inner array
tileAnimations[index] = new int[innerArraySize];
//assign the first item of the inner array
//TODO: make sure there is something in tokenBuffer first,
//then loop through it.
tileAnimations[index][innerindex] = atoi(strtok(tokenBuffer, ","));
//assign each item in the innerArray a value for
//the animation sequence.
for (innerindex++; innerindex < innerArraySize; innerindex++)
{
tileAnimations[index][innerindex] = atoi(strtok(NULL, ","));
}
/*buffString = new char[10];
delete [] buffString;*/
//increment index
index++;
}
}
code:
buffstring values = 1, 2, 3, 4 and then it crashes afterwards on the
4th iteration.
This works fine until the fourth iteration.
breaks on this line: buffString = new char[numLineItems];
The contents of the text file it is reading:
1,1,1,1,1,1,1,1,1,5,6,7,8,9
2,2,2,2,2,2,2,2,2
3,3,3,3,3,3,3,3,3
4,4,4,4,4,4,4,4,4
5,5,5,5,5,5,5,5,5
6,6,6,6,6,6,6,6,6
Any ideas on how to fix this or what the problem may be? Need to get
the entire file read in and not error out on the fourth iteration.
(errors out on line with 5's on it)
Code is below:
void DrawWorld::LoadParseFile()
{
//string to store data in LoadParseFile(ifstream*) function
theFile = new ifstream("Maps\\1.txt");
char currentCharacter = '\0';
char *tokenBuffer = "";
int arraySize = 0;
int index = 0, innerindex = 0;
if (!theFile)
{
exit(1); // terminate with error
}
//deterimine size of file
while(!theFile->eof())
{
currentCharacter = theFile->get();
//increment our arraySize if we have a \n
if (currentCharacter == '\n')
{
arraySize++;
}
}
//there is typically one more
//row in a file than carriage returns
arraySize++;
//clear eof state
theFile->clear();
theFile->seekg(0);
/*initialize our arrays
the tiles*/
tiles = new int[arraySize];
//whether the tile is enterable
enterable = new int[arraySize];
/*whether the character is displayed
behind or in front of the tile*/
tileBehind = new int[arraySize];
//for storing animation on/off 1 = yes 0 = no
tileAnimationOnOff = new int[arraySize];
//whether the animation repeats or not 1 = yes 0 = no
tileAnimationLoop = new int[arraySize];
//for storing the time to wait in between animation switches
tileAnimationFrequency = new int[arraySize];
//for storing the animations
tileAnimations = new int*[arraySize];
while (!theFile->eof())
{
char *buffString;
//get a line out of mapFile and assign
//it to buffstring
char lineCounter[1];
int numLineItems = 0;
int currentLocation = theFile->tellg();
lineCounter[0] = theFile->get();
//determine number of characters in current line
while(lineCounter[0] != '\n')
{
lineCounter[0] = theFile->get();
numLineItems++;
}
int test = theFile->tellg();
//redimension our array to the right size for the line
buffString = new char[numLineItems];
//drop back the same number of characters
//that we just read
theFile->seekg(currentLocation);
//get the exact number of characters we need
theFile->getline(buffString, 100);
//first item is the tile number
tiles[index] = atoi(strtok(buffString, ","));
//next is enterable
enterable[index] = atoi(strtok(NULL, ","));
//tilebehind
tileBehind[index] = atoi(strtok(NULL, ","));
//tileAnimationOnOff
tileAnimationOnOff[index] = atoi(strtok(NULL, ","));
//tileRepeat
tileAnimationLoop[index] = atoi(strtok(NULL, ","));
//animation speed
tileAnimationFrequency[index] = atoi(strtok(NULL, ","));
//deterimine size of the inner array
int innerArraySize = 0, i = 0;
//grab the remaining characters in this line
tokenBuffer = strtok(NULL, "");
//begin the while with the first character of tokenBuffer
//TODO: only do this if tokenBuffer has a value
currentCharacter = tokenBuffer[0];
//while through the character array
while(currentCharacter != '\0')
{
//we are counting the commas, this will
//give us an accurate measurement of the
//elements we will have in our inner array
if (currentCharacter == ',')
{
innerArraySize++;
}
i++;
//select the next item in the character array
currentCharacter = tokenBuffer;
}
//we'll have 1 more element than we have commas
innerArraySize++;
//allocate memory for our inner array
tileAnimations[index] = new int[innerArraySize];
//assign the first item of the inner array
//TODO: make sure there is something in tokenBuffer first,
//then loop through it.
tileAnimations[index][innerindex] = atoi(strtok(tokenBuffer, ","));
//assign each item in the innerArray a value for
//the animation sequence.
for (innerindex++; innerindex < innerArraySize; innerindex++)
{
tileAnimations[index][innerindex] = atoi(strtok(NULL, ","));
}
/*buffString = new char[10];
delete [] buffString;*/
//increment index
index++;
}
}