All pastes #592963 Raw Edit

Mine

public text v1 · immutable
#592963 ·published 2007-06-28 01:19 UTC
rendered paste body
Below is a poor noob-written function in MFC. What it does is take a file full of street address (3-4 lines long for the address itself, followed by another line containing a timestamp). It 'purges' any addresses that are older than x amount of days and re-writes the rest to a file.

The mystery is this: Why am I getting a blank line on the very first line whenever it re-writes the file?

void Purge::OnBnClickedOk()
{
	CString input, tmpMasterFile, error;
	FILE *master, *tmpMaster;
	time_t timestamp = time(NULL);
	const char *days, *tempMaster, *MasterFilePath = (LPCTSTR) MasterFile;
	int seconds;
	int whenprocs = 0;
	int i = 0; // the number of lines in the current address; minimal is 4.
	const int BUFSIZE = 1024;
	char buf[BUFSIZE];
	char *address1 = NULL, *address2 = NULL, *address3 = NULL, *address4 = NULL, *address5 = NULL;

	GetDlgItemText(IDC_AGE, input);

	days = (LPCTSTR)input;

	if (!validnumber(days))
	{
		AfxMessageBox("Please input a valid number of days.");
		return;
	}

	seconds = (int)days - 86400;

	// sanity checking
	fopen_s(&master, MasterFilePath, "r");

	if (master == NULL)
	{
		error.Format("ERROR: Unable to open %s.", MasterFile);
		EndDialog(IDD_PURGE);	
		AfxMessageBox(error);
		return;
	}
	
	fclose(master);
	
	tmpMasterFile = MasterFile + ".old";
	tempMaster = (LPCTSTR) tmpMasterFile;

	if (!MoveFile(MasterFile, tempMaster))
	{
		error.Format("ERROR: Unable to create a temporary file from %s.", MasterFile);
		EndDialog(IDD_PURGE);	
		AfxMessageBox(error);
		return;
	}

	// more sanity checking below
	fopen_s(&tmpMaster, tempMaster, "r");

	if (tmpMaster == NULL)
	{
		error.Format("ERROR: Unable to open %s.", tempMaster);
		EndDialog(IDD_PURGE);	
		AfxMessageBox(error);
		return;
	}

	fopen_s(&master, MasterFile, "wb");

	if (master == NULL)
	{
		error.Format("ERROR: Unable to open %s.", MasterFile);
		EndDialog(IDD_PURGE);	
		AfxMessageBox(error);
		return;
	}
	// end lots of sanity checking

	while (fgets(buf, BUFSIZE, tmpMaster))
	{

		// sanity check.
		strcpy_s(buf, strip_invalid_chars(buf));

		if (*buf == NULL) {

			if (i < 4 || i > 5) {
				// this is a bad address! it's either too short or too long.
			} else if (address1) {
				// successfully obtained full street address!

				// now check the timestamp
				if (address5 && validnumber(address5)) whenprocs = atoi(address5);
				else if (address4 && validnumber(address4)) whenprocs = atoi(address4);

				if (!whenprocs) {
					continue; // this address is bad!
				} else {
					// this address seems to be valid, check it's timestamp

					if (whenprocs > (timestamp - seconds)) {
						fprintf(master, "%s\r\n", address1);
						fprintf(master, "%s\r\n", address2);
						fprintf(master, "%s\r\n", address3);
						fprintf(master, "%s\r\n", address4);

						if (address5) fprintf(master, "%s\r\n", address5);
					}
				}
			}
			i = 0;
			whenprocs = 0;

			address1 = NULL;
			address2 = NULL;
			address3 = NULL;
			address4 = NULL;
			address5 = NULL;
		}
		i++;

		if (address1 == NULL) { address1 = _strdup(buf); }
		else if (address2 == NULL) { address2 = _strdup(buf); }
		else if (address3 == NULL) { address3 = _strdup(buf); }
		else if (address4 == NULL) { address4 = _strdup(buf); }
		else if (address5 == NULL) { address5 = _strdup(buf); }
	}
	fclose(tmpMaster);
	fclose(master);

	EndDialog(IDD_PURGE);
	error.Format("Process Complete! The old master file saved as %s", tmpMasterFile);
	AfxMessageBox(error);
}