#include <windows.h>
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "resource.h"
char* getString(HWND hwnd,int dlgItem)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, dlgItem));
if (len > 0)
{
TCHAR szBuffer[128] = { 0 };
int s = GetDlgItemText(hwnd, dlgItem, szBuffer, len + 1);
return szBuffer;
}
return "";
}
int getInt(HWND hwnd, int dlgItem)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, dlgItem));
if (len > 0)
{
BOOL bsuccess;
int Number = GetDlgItemInt(hwnd, dlgItem, &bsuccess, FALSE);
return Number;
}
return 0;
}
char* _personRecord(HWND hwnd)
{
int nameLength = GetWindowTextLength(GetDlgItem(hwnd, txtName)) + 1;
int addressLength = GetWindowTextLength(GetDlgItem(hwnd, txtAddress)) + 1;
int phoneLength = GetWindowTextLength(GetDlgItem(hwnd, txtPhone)) + 1;
int emailLength = GetWindowTextLength(GetDlgItem(hwnd, txtEmail)) + 1;
char* name = malloc(nameLength);
name = getString(hwnd, txtName);
int age = getInt(hwnd, txtAge);
char* address = malloc(addressLength);
address = getString(hwnd, txtAddress);
int zip = getInt(hwnd, txtZip);
char* phone= malloc(phoneLength);
phone = getString(hwnd, txtPhone);
char* email = malloc(emailLength);
email = getString(hwnd, txtEmail);
int length = nameLength + addressLength + phoneLength + emailLength + age + zip;
char* personRecord = malloc(length);
sprintf_s(personRecord , length, "Name: %s\nAge: %d\nAddress: %s\nZip: %d\nPhone: %s\nEmail: %s\n", name, age, address, zip, phone, email);
OutputDebugString("\nStart*********\n");
OutputDebugString(personRecord);
OutputDebugString("\nEnd**********\n");
return personRecord;
free(name);
free(address);
free(phone);
free(email);
free(personRecord);
}
void saveFile(char* record)
{
FILE* fp = NULL;
if(fopen_s(&fp,"contacts.txt", "a") == 0)
{
fprintf(fp, "\n");
fprintf(fp, record);
fclose(fp);
}
else
{
OutputDebugString("Op failed");
}
}
//Works 100 percent.
void clear(HWND hwnd)
{
SetDlgItemText(hwnd, txtName, "");
SetDlgItemText(hwnd, txtAge, "");
SetDlgItemText(hwnd, txtAddress, "");
SetDlgItemText(hwnd, txtZip, "");
SetDlgItemText(hwnd, txtPhone, "");
SetDlgItemText(hwnd, txtEmail, "");
}
BOOL CALLBACK EventHandler(HWND holdWindow, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch (Message)
{
case WM_INITDIALOG:
break;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case btnSave:
saveFile(_personRecord(holdWindow));
clear(holdWindow);
break;
case btnLoad:
//print all records to console
break;
case btnClose:
EndDialog(holdWindow, 0);
}
break;
case WM_CLOSE:
EndDialog(holdWindow, 0);
break;
default:
return FALSE;
}
return TRUE;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, EventHandler);
}
First real crack at a C program that really does anything of note. I'm sure I'm doing a ton wrong with memory management but I'm a newbie so please go easy on me. As far as my problem goes, sprintf is turning those strings into boxes when I print them out and save them to the file. I thought it could be my getString methods that are causing this problem but if I print a variable out after it's been called it works fine in the debug. I've tried adjusting how I'm allocation personRecord several times including making it static and NULL. I've also tried printing them individually. I'm at a complete loss at this point.
char* szBuffer[1024] = { 0 };
TCHAR was the wrong type and 128 was not enough memory to save the string properly. I adjusted how much memory could be used and it fixed the issue. I think the correct answer would be to malloc the data to the length of the string I took from the window but I'm not sure entirely. Thanks for everyone who posted.
char* getString(HWND hwnd,int dlgItem)
{
int len = GetWindowTextLength(GetDlgItem(hwnd, dlgItem));
if (len > 0)
{
char* record = malloc(len * sizeof *record + 1);
int s = GetDlgItemText(hwnd, dlgItem, record, len + 1);
return record;
free(record);
}
return "";
}