S
S
Stratimon2016-02-14 21:50:40
C++ / C#
Stratimon, 2016-02-14 21:50:40

Output in edit text from .txt file how?

I am studying WinApi c++ and the next task is to display in edit what is in the text file
, tabs, spaces are displayed, but there is no transition to the next line

char tt[1024];  
ifstream ifs(nameFile);//открываю файл 
string s;
s.assign((istreambuf_iterator<char>(ifs.rdbuf())), istreambuf_iterator<char>());//получаю весь текст 
strcpy( tt, s.c_str() );//string в char
ifs.close(); //закрываю файл
//edit
text = CreateWindow("edit","edit", WS_GROUP |
        WS_CHILD | WS_VISIBLE | ES_MULTILINE,
                    160,0,600,600,hWnd,(HMENU)101,
                    (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE),NULL);

//запись в edit
SetWindowText(text, tt);

Everything is fine in the console!
And is it possible to write all the text from the file at once to char

Answer the question

In order to leave comments, you need to log in

4 answer(s)
M
MiiNiPaa, 2016-02-15
@MiiNiPaa

SetWindowText expects a 2-byte string translation (0x0D 0x0A). ifstream will turn a 2-byte newline into a 1-byte for internal storage. You can read the file as binary:ifstream ifs(nameFile, "ios::binary");

P
Pavel Kaptur, 2016-02-15
@drem1lin

you write about WinApi, but you use streams yourself) for WinApi you need to do CreateFile, ReadFile, and then SetWindowText and everything will be right at once

A
Antony, 2016-02-15
@RiseOfDeath

Or, as an option, replace \n with std::endl (because std::endl must be the end-of-line character (characters) accepted for this system), or manually replace \n with \r\n

S
Stratimon, 2016-02-16
@Sratimon

Thanks everyone for the answer but I found another solution
I read the file line by line and add \r\n at the end of each line

#include <fstream>
char tt[1024]; // сюда запишем весь текст  
int readText(char nameFile[80]){ // принимает название файла 
    memset(tt, 0, sizeof(tt)); // чистим tt[]
    char buf[180]; // тут будет лежать строка из файла 
    char buf2[4]="\r\n"; // то что добавляем в конце файла 
    FILE *fp; //буфер файла
    fp = fopen(nameFile, "r"); // открываем файл 
     while (! feof(fp)){ // читаем файл построчно  пока он не закончиться 
          fgets(buf, 180,fp);  // записываем  строку в buf
          strcat(buf,buf2); // добавляем в конце \r\n 
          strcat(tt,buf); // записываем строку туда где будет лежать весь текст 
      }
        fclose(fp); // закрываем файл 
}

What can you say about what I did?
How it works suits me quite well.
Except for one thing, how can I make the function return tt without making it global?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question