Solved

CStdioFile and carriage returns within a line.

Posted on 2002-03-03
10
437 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
Comment Utility
Have you set the "Want Return" property on the edit box?
0
 
LVL 6

Expert Comment

by:peterdownes
Comment Utility
Sorry, I mean the "multiline" property.
0
 

Author Comment

by:jjjkkklll
Comment Utility
Yes.
0
 
LVL 49

Accepted Solution

by:
DanRollins earned 100 total points
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:jjjkkklll
Comment Utility
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
Comment Utility
I'll bet a nickle to a dollar that you are inserting:

     "\\r\\n"

Do I win?

-- Dan
0
 

Author Comment

by:jjjkkklll
Comment Utility
You owe me a nickel.
0
 
LVL 49

Expert Comment

by:DanRollins
Comment Utility
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
Comment Utility
>>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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Running shell command from Access 3 63
centeredAverage challenge 8 120
Detect file exist or not 3 76
unix example issues 18 44
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

771 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

12 Experts available now in Live!

Get 1:1 Help Now