Ok, I've tried literally over 100 things and still can't figure this out. I want to add strings that I've loaded from a text file to a list box. Here is my coding:
vector<string> spnV; //this is a global variable
BOOL LoadTextFileToEdit(LPCTSTR pszFileName)
{
ifstream inStream(pszFileName);
if(!(inStream.is_open()))
{
MessageBox(NULL, TEXT("Cannot open stream to file"), TEXT("INPUT ERROR"), MB_ICONERROR);
return FALSE;
}
while(!inStream.eof())
{
char sztemp[1000];
string stemp;
inStream.getline(sztemp, 1000);
stemp = sztemp;
spnV.push_back(sztemp);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
inStream.getline(sztemp, 1000);
char ch1, ch2;
inStream.get(ch1);
inStream.get(ch2);
if(!(ch1 == '*' && ch2 == '\n'))
{
MessageBox(NULL, TEXT("Cannot find appropriate data in file"), TEXT("INPUT ERROR"), MB_ICONERROR);
return FALSE;
}
}
inStream.close();
return TRUE;
}
This is the load function.
And here's a function that uses it (which also works):
void DoFileOpen(HWND hwnd)
{
OPENFILENAME ofn;
char szFileName[MAX_PATH] = "";
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(OPENFILENAME);
ofn.hwndOwner = hwnd;
ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
ofn.lpstrFile = szFileName;
ofn.nMaxFile = MAX_PATH;
ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
ofn.lpstrDefExt = "txt";
if(GetOpenFileName(&ofn))
{
LoadTextFileToEdit(szFileName);
}
}
And here's where this function is used:
case IDC_LOAD:
DoFileOpen(hwnd);
int nTimes = spnV.size();
for(int i = 0;i < nTimes; i++)
{
string* word = spnV[i];
int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)word);
// Here we are associating the value nTimes with the item
// just for the heck of it, we'll use it to display later.
// Normally you would put some more useful data here, such
// as a pointer.
SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, 1);
}
break;