I want to read text from a file and display it in the win32 Edit(ReadOnly). I am having problems converting from std::string to w_char*/LPWCSTR. The program reads the text from the textfile into a vector<std::string> object. I want to display each item in the vector on a new line inside the Edit. The displayed text is not what was read from the file though, e.g.
The original string: "Last year in august a bear was spotted in the norwood woods...",
the displayed text: "M1TELBbJM7StfoRRtfoAAtfoA8AtfoJJtfoJJtfoU9UtfoJJtfoAAtfoHWbMpsaA"

Code that recieves the vector and displays it in the edit:

//method used to append text to Edit
void appendTextToEdit( HWND Edit, LPCWSTR newText ){
    int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
    SendMessage(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
    SendMessage(hEdit, EM_REPLACESEL, FALSE, (LPARAM)newText);
}

void display_data(){  //function for displaying lines

    data = new vector<string>();   //vector holding the lines read from the file
    *data = *file->read_from_file();  //header file method (below)

        //loop for displaying the data on individual lines in the edit
        for(unsigned i = 0; i < data->size(); i++){
            wstring stemp = wstring(data->at(i).begin(), data->at(i).end());
            LPCWSTR line = stemp.c_str();
            appendTextToEdit(hEdit, /*(LPCWSTR)*/ line);
        }

        delete data;
    delete file;
}

In the header file: "FileMag.h", the method for reading from the file:

std::vector<std::string>* read_from_file(){  //method for reading from file

        if(!read_in->fail()){

            std::string line = "";
            point = new std::vector<std::string>();  //memory deleted in deconstructor

            while(!read_in->eof()){
                *read_in >> line;
                point->push_back(line);
            }

            read_in->close();  //close file
            return point;
        }else{
            file_ex er;    //custom error class extending std::exception
            er.set_msg("Could not access file");  //set error message to display
            throw er;
        }
        return NULL;
    }
Search_not
Deleted Member

All I need is a way to convert std::string to LPWCSTR, or maybe even const char* to const w_char*. I have tried google searching but came up with no results that actually work

Why not use std::wstring instead of std::string. Then you can call std::wstring.c_str() which will give you an wchar_t*.

I'm getting pages of errors when I try to read from the textfile into a std::wstring vector, at line 9 with the error: cannot bind 'std::basic_istream<char>' lvalue to 'std::basic_istream<char>&&'|. Also when I try to assign values to the data in the vector e.g. data->at(0) = "1"; = error: invalid conversion from 'const char*' to 'wchar_t' [-fpermissive]|. Should I read from the file into a std::string and then convert to std::wstring? (I changed all the std::string vectors and variables to std::wstring)

What is the type of read_in? If it an ifstream you need to make it wifstream. If you are going to write a program that supports unicode then you should convert the entire program to use unicode. all instances of string, ifstream, ofstream should become their wide counterparts.

read_in is std::ifstream, but I changed everything as you said and I am not getting errors on the file streams anymore, thanks. I am still getting errors when I try to assign values to my std::wstring vector... How do I convert const char*/std::string to wchar_t*? e.g.
data->at(0) = "1"; returns the error: error: invalid conversion from 'const char*' to 'wchar_t' [-fpermissive]|. I change the data in the vector to overwrite the data in the textfile once the user updates his/her settings

Edited 1 Year Ago by Search_not: typing error

Its these two functions:

void set_data(){
    data->at(0) = "1";
    data->at(1) = c_dir;   //current directory saved in std::wstring
    data->at(2) = "non";
    data->at(3) = "valid";
}



void display(const unsigned short day_num){  
    try{
        string file_name = file_path[2] + boost::lexical_cast<std::string>(day_num) + ").txt";
        file = new Files(file_name, READ_FILE, IOS_IN);

            data = new vector<wstring>();
            *data = *file->read_from_file();

                for(unsigned i = 0; i < data->size(); i++){
                    std::wstring line = data->at(i);
                    appendTextToEdit(hEdit, /*(LPCWSTR)*/ line);
                }

            delete data;
        delete file;
    }catch(file_ex& er){
        clear_edit_text(hEdit);
        appendTextToEdit(hEdit, (LPCWSTR) er.what());
    }catch(exception e){                                    //backup catch for in-case
        cout << "exception caught: " << e.what() << endl;
        exit(FILE_ACCESS_ERROR);
    }
}

Edited 1 Year Ago by Search_not

I am getting invalid conversion errors on lines 2-5 and then another one on line 19. Line 19 error message: error: cannot convert 'std::wstring {aka std::basic_string<wchar_t>}' to 'LPCWSTR {aka const wchar_t*}' for argument '2' to 'void appendTextToEdit(HWND, LPCWSTR)'|
Will changing line 19 wstring to a pointer type solve the problem?

Edited 1 Year Ago by Search_not

You need to use L before string literals now that you are using wstrings for lines 2-5.

Your for loop that is giving you your second error:

for(unsigned i = 0; i < data->size(); i++){
    std::wstring line = data->at(i);
    appendTextToEdit(hEdit, /*(LPCWSTR)*/ line);
}

Can be rewritten to be:

for(unsigned i = 0; i < data->size(); i++){
    appendTextToEdit(hEdit, line.c_str());
}

Edited 1 Year Ago by NathanOliver

It works perfectly! Thanks!

@search_not I made a mistake in the corrected for loop. The code should be

for(unsigned i = 0; i < data->size(); i++){
    appendTextToEdit(hEdit, (data->at(i)).c_str());
}

That way you dont have to create the temporary string line.

Thanks, the less code the better. I am still getting garbled english displayed, as described in my first question above... I think its in my appendTextToEdit function(below), in which I cast the wstring to LPARAM. e.g.

void appendTextToEdit( HWND Edit, LPCWSTR newText ){
    int TextLen = SendMessage(hEdit, WM_GETTEXTLENGTH, 0, 0);
    SendMessage(hEdit, EM_SETSEL, (WPARAM)TextLen, (LPARAM)TextLen);
    SendMessage(hEdit, EM_REPLACESEL, FALSE, (LPARAM) newText);
}

Am I doing something wrong in appending lines to my win32 EDIT? Would using a RichEdit make my life easier??

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