Solved

CStdioFile and carriage returns within a line.

Posted on 2002-03-03
10
442 Views
Last Modified: 2013-11-20
I need to be able to create a text file in Notepad and have my program read the lines from the text file into variables.  Right now I'm using CStdioFile to ReadString the lines into variables and everything is working pretty good.

My question is: is there a way to add something within the line in the text file that would cause a carriage return within that line as it's put into an edit box?  In other words, let's say I have three lines in the text file and when I use ReadString on this file, I load those three lines into separate variables.  Now, within the third string, I want carriage returns to show up in the edit box when I load that string.  I've tried adding \r\n to the line in the text file. I've tried using CFile::typeText as a parameter of CStdioFile.  I've tried replacing \r\n within the resulting CString variable to "\r\n" and other crazy stuff.  So far, I haven't been able to get carriage returns to happen within a line.

Is it possible to add something to a line in a text file that can be read by CStdioFile::ReadString to put carriage returns within that line?  Alternatively, if I can replace, say, "\r\n" with something within the CString variable after loading the line from the text file, what can I put in there to cause a carriage return within the edit box?  I hope this makes sense.  If it doesn't, I'll clarify. Thanks.
0
Comment
Question by:jjjkkklll
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 6

Expert Comment

by:peterdownes
ID: 6838247
Have you set the "Want Return" property on the edit box?
0
 
LVL 6

Expert Comment

by:peterdownes
ID: 6838252
Sorry, I mean the "multiline" property.
0
 

Author Comment

by:jjjkkklll
ID: 6838429
Yes.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
ID: 6838485
How are you placing the text into the textbox?  I assure you that a string such as

        "line1\r\nline2\r\nline3"

will appear as three separate lines in a text edit control if you use the SetWindowText function or if you use the normal Dialog DDX transfer mechanism ( UpdateDate(FALSE) ).

The most common error is forgetting to set the Multiline attribute in the edit control, as peterdownes suggested.

It is also possible that you have forgotton to add the "\r\n" to the ends of the lines that you read via

CString s;
CStdioFile::ReadString(s).  

That version of the function strips the eol chars, so you need to add them back.  

Side note:  The file itself probably has lines that end in '\r\n' (0x0d 0x0a) but ReadString calls fgetts() which replace that DOS/WIN-specific EOL with '\n' which is universal (well anyway Macintosh an Unix nerds expect it).  Then ReadString checks the final character and if it is '\n', it truncates it from the string by replacing it with '\0'

Finally, are you sure that you have inserted the '\r\n' correctly where you think that you have?  Look at the text in the debugger, before and after executing the code that does the insert.

-- Dan
0
 
LVL 6

Expert Comment

by:Triskelion
ID: 6839791
What do you see when you add the string with the "\r\n" attached to it?
Are you saying that it's all on one line?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:jjjkkklll
ID: 6841924
I think the fact that I'm loading the CString by using the CStdioFile::ReadString fn is causing the problem.  I've tried a million different versions of \r\n and it always prints within the line inside the box ("Line 1.\r\nLine 2").  I tried adding \n and \r\n to the end of the string before putting it in the box.  I never got this to work.

However, I did find a solution that works for me.  I changed the box to a rich edit control, added the rtf header in front of the string and after and then, when I add \par\par to the text in the text file, it works fine.  It prints with the line breaks within the box.  Why?  Not sure.  No time to figure it out right now.  Thanks everyone for your help.  Not sure who to give the points to.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6843734
I'll bet a nickle to a dollar that you are inserting:

     "\\r\\n"

Do I win?

-- Dan
0
 

Author Comment

by:jjjkkklll
ID: 6845342
You owe me a nickel.
0
 
LVL 49

Expert Comment

by:DanRollins
ID: 6845751
Then I'll wager a pickle to a lemon, that you inserted a backslash, then inserted an r, etc...  You would need to have a string literal in your ptrogram so that the compiler will convert \r\n into the two desired control characters (0x0d, 0x0a).

>>and it always prints within the line inside the box ("Line 1.\r\nLine 2").

If you see the four literal characters
   \r\n
on the screen, it means that the character \, followed by the character r, followed by the character \, followed by the character n have been inserted into the text.  

If you see two small squares, it means that there are two undisplayable characters (probably CR LF) and the problem is that the display element does not take action (e.g. in a single-line edit control).

-- Dan
0
 

Author Comment

by:jjjkkklll
ID: 6848243
>>You would need to have a string literal in your ptrogram so that the compiler will convert \r\n into the
two desired control characters (0x0d, 0x0a).

I think you nailed it on the head, there. My next try would've to put something like ~~ anywhere I wanted a line break and them do a CString replace, replacing that with \r\n, but I've moved on.  Thanks for the help, Dan.  And keep your pickle.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Include multiple hostnames in this command? 3 53
move a line in eclipse 3 82
lucky13 challenge 11 123
pre4 challenge 19 94
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now