Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help with code used for reading in a file

Status
Not open for further replies.

lemming999

Programmer
Mar 11, 2002
12
IE
Hi, very novice programmer here, When I run this function, it prints out the first messageBox (ie when count == 0 but it doesn't go to the next messageBox, i know that they're are more than 1 entry in the file I'm loading.

any ideas, its very similar to code that i got to work with a static text outputting program




while (!DataIn.eof()) // While more entries
{
int count;
DataIn.read((char*)&entry,sizeof(DataIn));

if(DataIn.eof()) //eof not set till after end
break;

if(count == 0)
{
strcpy(First.name,entry.name);
MessageBox(hwnd, First.name, TEXT("HelloMsg"), 0);
count++;
}
else
{
strcpy(Current.name,entry.name);
MessageBox(hwnd, Current.name, TEXT("HelloMsg"), 0);

}
}
DataIn.clear(); // Reset eof flag
return TRUE;
}




Thanks,
Brian
 
Your problem is that the count variable is inside the while loop, so each iteration re-initialises it back to zero.

your code ...

while (!DataIn.eof()) // While more entries
{
int count;
DataIn.read((char*)&entry,sizeof(DataIn));
...
}

should read

int count = 0;
while (!DataIn.eof()) // While more entries
{
DataIn.read((char*)&entry,sizeof(DataIn));
...
}

another novice mistake would be to make the count variable a static variable.
eg:

while (...)
{
static int count = 0;
...
}

This is sloppy because, while it will stop the re-initialisation on each loop iteration, it causes resources to be permanently alloocated for the liofe of the program, rather than the life of the function doing the WHILE. It would be bad programming.


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top