Why are my visible line ending different?

38 views
Skip to first unread message

Andrew Truckle

unread,
Apr 25, 2024, 9:58:24 AMApr 25
to scintilla-interest
I understand that there are three flavours of line endings:
  1. LF
  2. CR
  3. CRLF
When I open my file with NotePad++ the line endings are CRLF:

EOL1.bmp

But, when using the Scintilla control they are CR:

EOL2.bmp

This is my code for reading the file:

void CScintillaHelper::ReadFile(const CString& filePath)
{
std::ifstream file(filePath);
if (file.is_open())
{
m_edit.ClearAll();
std::string line;
while (std::getline(file, line))
{
m_edit.AppendText(line.size(), line.c_str());
m_edit.AppendText(1, L"\r\n");
}
file.close();
m_edit.SetSavePoint();
}
}


Why is this? Not a big issue.

Neil Hodgson

unread,
Apr 25, 2024, 5:40:25 PMApr 25
to Scintilla mailing list
Andrew Truckle:

> m_edit.AppendText(1, L"\r\n");

"\r\n" contains 2 characters, not 1.

Neil

Andrew Truckle

unread,
Apr 26, 2024, 12:59:15 AMApr 26
to scintilla-interest
Hi Neil

Thanks for your response. But, I specifically used \r\n in my code (CR LF).. Notepad++ correctly show these line endings as CRLF. My question is about why my attempt at displaying the same file in Scintilla is only showing CR for the same file?

Andrew Truckle

unread,
Apr 26, 2024, 4:00:25 PMApr 26
to scintilla-interest
Another resolved issue. Changing the way I read the file corrected the line ending matter:

void CScintillaHelper::ReadFile(const CString& filePath)
{
ifstream ifs(filePath, ios::in | ios::binary | ios::ate);

const ifstream::pos_type fileSize = ifs.tellg();
ifs.seekg(0, ios::beg);

vector<char> bytes(fileSize);
ifs.read(bytes.data(), fileSize);

m_edit.AppendText(fileSize, std::string(bytes.data(), fileSize).c_str());
m_edit.SetSavePoint();
}

Reply all
Reply to author
Forward
0 new messages