Stopping pointer reference

I have a function that converts data from a file to a string using getline() then to char* then to wchar_t* and finally to vector<wchar_t*>, then returns that final vector to the main function. I need the function to go through this route because I'm programing in win32 api unicode, but .txt files use ansi characters.

Now I'm having the problem that every wchar_t* variable in my vector is equal to the last input retrieved from getline. I'm assuming that's because of pass-by-reference due to all the pointers. (sorry if the term is wrong, I'm learning through books and tutorials on my own)

I'm wondering how do you stop pass by reference between variables?
I need the function to go through this route because I'm programing in win32 api unicode


That's probably a bad idea.

If your text file is ANSI, then use the ANSI WinAPI functions. No need to use the Unicode version if you're not going to use Unicode strings. Let Windows do the conversion for you.

Remember that all WinAPI functions which take strings come in 3 forms:

1) TCHAR form --- normal function name: MessageBox

2) wide (wchar_t) form --- add a 'W' at the end of the file name: MessageBoxW

3) narrow/ANSI (char) form --- add an 'A' to the end of the file name: MessageBoxA


If you are using wchar_t's, you should be calling the 'W' version of the function. Although you probably are using the TCHAR version by mistake. Break that habit. Only use the TCHAR version if you are using TCHARs.

With this you can simplify what you're doing greatly, and just pass the normal strings to the 'A' WinAPI functions.



Anyway... that's the solution I recommend. Simpler and "the right thing to do". That said... as for your actual question:

Now I'm having the problem that every wchar_t* variable in my vector is equal to the last input retrieved from getline. I'm assuming that's because of pass-by-reference due to all the pointers.


Pointers are not strings. They're pointers. If you have a wchar_t* that doesn't mean you have a string.

wchar_t*'s typically point to an array of wide characters. This array is the string. If you want multiple strings, you will need to have multiple arrays, with each pointer pointing to a different array.

Or... you can just use strings and not deal with arrays or pointers. std::wstring will work for wide characters... but if you go with my previous advice you can just use the normal std::string and not deal with wide characters at all.
I'm a bit confused, I opened the file using ifstream because I couldn't find a win32 version of getline. Can getline be used with a win32 file handle? Or else is there a win32 version of getline out there somewhere? Or maybe a wide version for getline...?

I opened the file using ifstream because I couldn't find a win32 version of getline


That's fine. ifstream and getline are fine to use. Keep using them.

Can getline be used with a win32 file handle?


No.

Or else is there a win32 version of getline out there somewhere?


Possibly, but don't bother with it... if it exists it will just be harder to use and won't do anything extra for you.

Or maybe a wide version for getline...?


Let's back up for a minute here. Why do you need a wide version of anything?

I'm not sure I completely understand what the goal here is. I'm assuming you have a plain ASCII text file that you want to read... and the only reason you need to use wide strings is to pass them to some WinAPI function like MessageBox or SetWindowText or something similar.

Is my assumption correct? Or is there some other reason why you need wide characters?

My suggestion is to not use wide characters at all. Unicode support is nice and all, but if you're only going to be reading ASCII text files then adding it doesn't really do anything for you because you'll never use it.
Last edited on
Topic archived. No new replies allowed.