Probleme mit GetLine() in CRichedit
-
Hi!
Ich verwende die Methode GetLine() um meinen Text zeilenweise in einen Buffer zu speichern:
/// int i, nLineLength, nLineCount = m_FehlerWarnungAusgabe.GetLineCount(); CString strText, strLine; for (i=0;i < nLineCount;i++) { nLineLength = m_FehlerWarnungAusgabe.LineLength(i); m_FehlerWarnungAusgabe.GetLine(i, strText.GetBuffer(nLineLength)); strText.ReleaseBuffer(nLineLength); strLine.Format(TEXT("line %d: '%s'\r\n"), i, strText.GetBuffer(0)); afxDump << strLine; } ///m_FehlerWarnungAusgabe ist ein Objekt meines CRicheditfensters. Das Problem ist, dass der Text in strText nicht richtig gespeichert wird. In der Variable befinden sich nur so komische Rechtecke.
Kann mir da jemand helfen pls?
-
Es ist zeitweilig extrem effizient die Dokumentation zu lesen!
Zitat:
lpszBuffer
Points to the buffer to receive the text. The first word of the buffer must specify the maximum number of bytes that can be copied into the buffer./// int i, nLineLength, nLineCount = m_FehlerWarnungAusgabe.GetLineCount(); CString strText, strLine; for (i=0;i < nLineCount;i++) { nLineLength = m_FehlerWarnungAusgabe.LineLength(i); TCHAR *buffer = strText.GetBuffer(min(sizeof(WORD),nLineLength)); *reinterpret_cast<WORD*>(buffer) = nLineLength; m_FehlerWarnungAusgabe.GetLine(i, buffer); strText.ReleaseBuffer(); strLine.Format(TEXT("line %d: '%s'\r\n"), i, buffer); afxDump << strLine; } ///
-
Wenn man weiter liest, kommt da noch:
nMaxLength
Maximum number of characters that can be copied into lpszBuffer. The second form of GetLine places this value into the first word of the buffer specified by lpszBuffer.
Damit wird die Funktion zu:
/// int i, nLineLength, nLineCount = m_FehlerWarnungAusgabe.GetLineCount(); CString strText, strLine; for (i=0;i < nLineCount;i++) { nLineLength = m_FehlerWarnungAusgabe.LineLength(i); m_FehlerWarnungAusgabe.GetLine(i, strText.GetBuffer(nLineLength),/**/nLineLength); strText.ReleaseBuffer(nLineLength); strLine.Format(TEXT("line %d: '%s'\r\n"), i, strText.GetBuffer(0)); afxDump << strLine; } ///The copied line does not contain a terminating null character.
-> das heißt, du mußt entweder das \0 manuell noch anhängen oder die Größe direkt an ReleasBuffer() übergeben.