CStdioFile and carriage returns within a line.

Posted on 2002-03-03
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.
Question by:jjjkkklll
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 2
  • +1

Expert Comment

ID: 6838247
Have you set the "Want Return" property on the edit box?

Expert Comment

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

Author Comment

ID: 6838429
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 49

Accepted Solution

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


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;

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

Expert Comment

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?

Author Comment

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.
LVL 49

Expert Comment

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


Do I win?

-- Dan

Author Comment

ID: 6845342
You owe me a nickel.
LVL 49

Expert Comment

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
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

Author Comment

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.

Featured Post

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
WinWaitActive parameters 12 31
Excel file not created as expected 7 95
Message not shown 5 87
Way to decrease size of apk file 9 112
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
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.
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

730 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