First of all, I want to apologize for the reposting.
Hi, I'm having trouble with the following function, getData( ). It's supposed to load an array x with data from file f, returning cursize, the number of elements loaded into x. This getData( ) function will only be called if the filename the user enters is a valid one. Otherwise, the main will just terminates the program. And if the file is opened successfully, then getData will either return a boolean value FALSE (Overflow) or TRUE (Data from the file has been loaded into the X array) back to main. I also include my main below getData( ) function. Overflow should only occur when cursize is larger than maxsize (15) and there is still data in the file that has not been loaded.
The PROBLEM I'M HAVING is that I have the codes compiled successfully, but no matter if it's an empty file, a file with less than 15 integers, or a file with 20 integers (overflow), it ALWAYS says that it's overflowed. Please help.
Boolean getData( FILE* f, int x, int* cursize, int maxsize )
char format = "%d";
mov ebx, x
mov esi, cursize
mov dword ptr [esi], 0 //cursize = 0
push esi //save ptr to cursize
push ebx //save ptr to x[cursize]
lea ebx, hold //save the address
push ebx //fscanf's 3rd param &d
lea ebx, format //save the format
push ebx //fscanf's 2nd param "%d"
call fscanf //fscanf(f, "%d", &d)
add esp, 12 //clear param off stacks
pop ebx //restore saved registers
mov eax, hold
cmp eax, EOF //the end of the file yet?
jne Next //if not, keep going
mov eax, 1 //Finish loading, if cursize is 0,
//this file is empty
jmp Done //return TRUE
mov ecx, maxsize //cursize > 15?
cmp dword ptr [esi], ecx //check overflow
jg Ovfl //cursize > maxsize
mov [ebx], eax //x[cursize] stores that value
inc dword ptr [esi] //cursize++
add ebx, 4 //next element
Ovfl: //Not EOF and overflowed
mov eax, 0 //return FALSE
/*THIS IS MY MAIN FUNCTION*/
#define MAX 15
int SECRET_NUM = 100; /* WHAT IS THIS FOR? */
int lowest, highest, avg, median;
FILE* dataFile; /* internal file pointer for the file */
char filename; /* string to hold the filename */
/* Ask the user for a filename, then open the data file.
Be sure to check that the open was successful.
If not, display a message, and terminate the program.
printf("Please enter a file name: ");
dataFile = fopen(filename, "r");
if ( dataFile != NULL )
loadOK = getData( dataFile, x, &cursize, MAX );
/* If the load was not OK, we ran out of room.
Display a message, and terminate the program.
if ( loadOK == 0 )
printf("Fail to load data, OVFL occurred.\n");
/* Array loaded successfully */
/* If there were zero elements in the data file, display a
message and terminate the program.
if ( cursize == 0 )
printf("The file is empty. Program terminated\n");
sortData( x, cursize );
displayGrades( x, cursize );
doStats( x, cursize, &lowest, &highest, &avg, &median );
displayStats( cursize, lowest, highest, avg, median );
printf("Fail to open file.\n");
printf("\n\nSECRET_NUM SHOULD BE 100: %d\n", SECRET_NUM);
Thanks for any help guys.