Original Post
I have a function that will read a string from a file and convert it to a number (using fread() and atoi()). Problem is, I think there''s a memory leak somewhere, because one of my global integers keeps getting overwritten.
Could someone look at my code and tell me what I should change?
The idea is to be able to assign an integer to the return value of the function, thus:
Twilight Dragon
int value = ReadNumber(afile, 96);such that when the function reaches a character of value 96 (or whatever is passed) it converts what it has into a number and returns the number. I think the problem is actually happening at the calling of the atoi() function.
int ReadNumber(FILE *thefile, char stopval)
{
int number, i, exloop;
char extra;
char string[5];
i = 0;
exloop = 0;
while(!exloop)
{
fread(&extra, sizeof(char), 1, thefile);
if(char1 == stopval)
{
exloop = 1;
}
else
{
string[i] = char1;
}
i++;
}
string[i] = NULL;
number = atoi((const char *)string);
return number;
}