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;

OMG what in the world are all those getline() calls???? If you want to put all those strings into the vector then use a loop! Use a string object to read the string and delete that damned character array (sztemp) which just takes up useless stack space. Replace lines 19 thru 75 with this simple loop.

string line;
while( getline(inStream, line) )
{
    spnV.push_back(line);
}

I didn't make the function; I'm just supposed to use it.

Whoever made that function is out of their mind -- it doesn't work as written because it doesn't load the strings into the vector.

yeah it does(if you're looking for the include files, well, I didn't put near all the coding in; only the necessary stuff).

what are you talking about. My original question had nothing to do with the function... It all had to do with the contents of the for loop. That's where I've narrowed the problem down to.

ok if you want, here's all the code:

the .cpp file:
<snipped because it causes major browser problems>

You mean this loop?

for(int i = 0;i < nTimes; i++)
							{
								string* word = spnV[i];

What is the value of nTimes? Why are you using a string pointer? spnV is an array of strings, not pointers.

I think that should be like this: I like to use iterators because I have had a few problems with just using size(), especially if some of the strings have been deleted from the vector. Note: assume hwnd is a handle to the listbox control.

vector<string>::iterator it = spnV.gegin();
for(; it != spnV.end(); it++)
{
   SendMessage(hwnd, LB_ADDSTRING, 0, (*it).c_str());
}

}

I thought you said that LoadTextFileToEdit() function worked? Well, the function you posted can't work the way you think it does -- see my previous post about how to correct it.

And you might want to review this example program.

I replaced my for loop and got this error:
error C2039: 'gegin' : is not a member of 'std::vector<_Ty>'
Also, right now I am looking at that example.

You will have to post the code you changed. You obviously misspelled something because there is no such function as 'gegin'. Did you mean getline() ?

Well I just copied and pasted what you had so I guess that must have been the problem.

nope that's not it because now I get this error:
error C2039: 'getline' : is not a member of 'std::vector<_Ty>'

that error was fixed thankyou, but i still have one more that has to do with the 4th parameter of the SendMessage function:
error C2664: 'SendMessageA' : cannot convert parameter 4 from 'const char *' to 'LPARAM'

I never really understood what type is LPARAM and WPARAM? what are they supposed to be? like ints or something? I've looked on msdn but never found anything.

Well this message came with it too:
There is no context in which this conversion is possible

This is not a workable solution, but it shows how to typecast that last parameter. Compiled with vc++ 2008 Express.

#include <iostream>
#include <windows.h>
#include <vector>
#include <string>
using namespace std;

int main(int argc, char **argv)
{
    HWND hwnd = 0;
    vector<string> spnV;
    vector<string>::iterator it = spnV.begin();
    for(; it != spnV.end(); it++)
    {
        SendMessage(hwnd, LB_ADDSTRING, 0, (LPARAM) (*it).c_str());
    }
    return 0;
}

Thank you so much. I put it in and everything works great!!! Thank you so much!!!

This question has already been answered. Start a new discussion instead.