Email [email protected] or Yahoo Messenger-Falazar

Someone please help, I am trying to edit a textfile, and save it back, but am getting many memory errors, I cannot get GlobalReAlloc to work correctly.. is there a better nicer way for all this to work.,... Thanx

Jams

BOOL doLoginLogout(HWND hwnd, const char* code)
{
// open file, check for correctness
LPSTR FileText = LoadTextFile("sample.txt");
string FirstName, LastName;

// search file until id is found...
int num = 0, place = 0;
string line, line2;
char code2[80];
int found = 0;
char str[800];
float hours;

do {
num++;
line = GetNLine(FileText, place);
// compare the login code now...

line2 = GetItemNum(line.c_str(), 3, hwnd);
sprintf(code2, "%s", line2.c_str());
// MessageBox(hwnd, code2, "Number Notice", MB_OK | MB_ICONINFORMATION);
if (line.length() > 0 && strcmp(code, code2) == 0) found = 1;
}
while (!found && line.length() > 0);
// functionilze the above

if (!found)
{
MessageBox(hwnd, "ID Unrecognized, Try Again", "Notice", MB_OK | MB_ICONINFORMATION);
GlobalFree(FileText);
// dont clsoe box now...
return FALSE;
}

// Part two, log the login to file...
FirstName = GetItemNum(line.c_str(), 2, hwnd);
LastName = GetItemNum(line.c_str(), 1, hwnd);

struct tm *newtime;
time_t t;
t = time(NULL);
newtime = localtime(&t);
char tstr[80];
char tstr2[80];

sprintf(tstr, "%02d/%02d/%d %02d:%02d\r\n", newtime->tm_mon+1, newtime->tm_mday, newtime->tm_year+1900, newtime->tm_hour, newtime->tm_min);
MessageBox(hwnd, tstr, "TIME NOTICE!", MB_OK | MB_ICONINFORMATION);

// load the current employees list and search to see if logged in
LPSTR LoginFile = LoadTextFile("login.txt");
num = isLoggedIn(LoginFile, code, hwnd);

if (num)
{
MessageBox(hwnd, "CALLING LOGOUT!", "Logout NOTICE!", MB_OK | MB_ICONINFORMATION);
// Log them Out ... move to archived
line = GetRecordNum(LoginFile, num);
// get second part of record, that is time
line = GetItemNum(line.c_str(), 2, hwnd);
sprintf(tstr2, "%s", line.c_str());

int siz = lstrlen(FileText);
siz -= strlen(line.c_str());
// set the flag to moveable or some such crap
FileText = (char*)GlobalReAlloc(FileText, siz, GMEM_MOVEABLE); // siz rep 500
siz = lstrlen(FileText);

// delete line from currect logins
ChangeRecordNum(LoginFile, num, "");

// do a subtraction now, and tell them how long they worked
hours = subTimeF(tstr, tstr2); // find the time difference

// put in their archive file, by month

sprintf(str, "%s %s Login time: %s LogOUT, at %s, for %5.2f hours as is appropo", FirstName.c_str(), LastName.c_str(), tstr, tstr2, hours);
}
else
{
MessageBox(hwnd, "LOGIN CALLED!", "LOGIN NOTICE!", MB_OK | MB_ICONINFORMATION);
// Log them In
sprintf(str, "%s\t%s", code, tstr);

int siz = lstrlen(FileText) + 1;
siz += lstrlen(str);
// set the flag to moveable or some such crap
FileText = (char*)GlobalReAlloc(FileText, siz, GMEM_MOVEABLE); // siz rep 500
if (FileText == 0) MessageBox(hwnd, "ERROR COULDNT REALLOC THE DATA SPACE!!!!!", "ERROR Notice", MB_OK | MB_ICONINFORMATION);

if (lstrlen(LoginFile) == 0) strcpy(LoginFile, str);
else strcat(LoginFile, str);

sprintf(str, "%s %s You have been logged IN at %s", FirstName.c_str(), LastName.c_str(), tstr);
}
SaveTextFile("login.txt", LoginFile);
MessageBox(hwnd, str, "Login/Logout Notice", MB_OK | MB_ICONINFORMATION);

// Free Up the Memory
GlobalFree(LoginFile);
// global free is still giving an error, just for people logging in...

return TRUE;
} // doLoginLogout