?
Solved

CStdioFile and carriage returns within a line.

Posted on 2002-03-03
10
Medium Priority
?
469 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
[X]
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
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
Moving data to the cloud? Find out if you’re ready

Before moving to the cloud, it is important to carefully define your db needs, plan for the migration & understand prod. environment. This wp explains how to define what you need from a cloud provider, plan for the migration & what putting a cloud solution into practice entails.

 
LVL 49

Accepted Solution

by:
DanRollins earned 300 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
 

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

Technology Partners: 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!

Question has a verified solution.

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

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…
In this post we will learn different types of Android Layout and some basics of an Android App.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Suggested Courses

752 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