Accessing a Specific Text Line Inside CEditView :: MFC

kuphryn

Senior member
Jan 7, 2001
400
0
0
Hello.

I have a program running under doc/view single doc architecture. When the user opens a text file, the program displays the data from the text file in its original form. Special thanks to Tychom for the key technique.

When the program first opens the file, it saves data into a CStringList. However, if the user make changes to one or more lines of text, I have not been able to access the updated text.

For example:

Original text:
---
a
b
c
---
Modified text:
---
az
b
cy0
---

I can get the updated text using GetWindowText(), but that function only returns a specific line parallel to the cursor. What if the user add one or more new lines? I have no way to accessing a specific line.

There is a function in CEdit, getline(). The first parameter is the line index. The second is a reference to LPTSTR. I tried to use that function to get a specific line, but the program crashed with an error "bad pointer."

LPTSTR text;
CEditCtrl::getline(1, text); // this program crashes here every time

Is there a way to access a specific line inside CEditView not dependent on where the cursor location?

Thanks,
Kuphryn
 

Adrian Tung

Golden Member
Oct 10, 1999
1,370
1
0
It's probably crashing because you're not giving it the right input. LPCTSTR is a typedef for const char *, you have to pass in a pointer to a string in order to use it.

i.e.
char szString[256];

then use GetLine(1, szString, 255);

If you just use GetLine(1, szString) then the first word of your string must specify the maximum size of the string.


Hope that helps,
:)atwl
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Thanks!

I tried that line, but it crashed again. However, this time it did not refer to "bad pointer." Maybe there is another problem somewhere. I will debug it now and post an update soon.

Kuphryn
 

kuphryn

Senior member
Jan 7, 2001
400
0
0
Okay. Your code works great.

Thanks!

I have one question.

In the line:

// char szString[256];

is 256 the maximun number of characters in *that line*?

I would like to get everything on that line.

In general, is 256 referring to bytes or characters (characters * bytes = maxsize?).

Kuphryn
 

DaveSimmons

Elite Member
Aug 12, 2001
40,730
670
126
Yes :)
so you probably should
#define MAX_LINE_LEN 1023
and change the code to

char szString[ MAX_LINE_LEN + 1 ] ;

GetLine( line, szString, MAX_LINE_LEN ) ;