Answer the question
In order to leave comments, you need to log in
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);
Answer the question
In order to leave comments, you need to log in
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");
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
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
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); // закрываем файл
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question