Solved

writing text to a new line.

Posted on 2003-11-03
11
681 Views
Last Modified: 2013-11-20
hi experts

i want to write some set of lines to a file.

i am using the CArchive and Cfile mfc classes.

i use the archive.writeString("\n"); to try and seperate the next write to a new line. however, when i check my file after running the program i find all the text writen to the same line.

i want to know how to move to the next line to write my text and not on the same line.
thank u very much

codechewer
0
Comment
Question by:codechewer
11 Comments
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 20 total points
ID: 9671364
Use CStdioFile class which has WriteString function.
0
 
LVL 32

Expert Comment

by:jhance
ID: 9671395
I think your problem is in the interpretation on the "\n" character.  The CArchive class treats this DIFFERENTLY than the CStdioFile class.  You want it interpreted as a NEWLINE rather than a binary "\n" character.  So, as AlexFM says, use a class to output it that properly translates this pseudo-character to what you really want in the output file.

Alternatively, you can output a "raw" CR LF pair using the hex values 0x0D and 0x0A.
0
 

Author Comment

by:codechewer
ID: 9671427
CArchive also has WriteString function and that is what i'm using

it doesn't write to next line.

e.g

      CArchive arStore(&myFile, CArchive::store);

      // Write to the archive file to store it.
      arStore.WriteString( "hello," );
      arStore.WriteString( "\n" );
      arStore.WriteString( "i am not doing badly and you?" );
      arStore.WriteString( "\n" );
      arStore.WriteString( "yours sincerely," );
             // Close the storing archive
      arStore.Close();

the output appears as:
hello [] i am not doing badly  and you? [] yours sincerely

but i want it to be:

hello,
i am not doing badly and you?
yours sincerely

codechewer
0
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 32

Accepted Solution

by:
jhance earned 30 total points
ID: 9671496
Instead of:

     arStore.WriteString( "\n" );

try:

     char buf[] = {0x0d, 0x0a};
     arStore.Write(buf,2);
0
 
LVL 32

Expert Comment

by:jhance
ID: 9671544
Note that your use of CArchive is INAPPROPRIATE here!  To QUOTE the SDK:

"The CArchive class allows you to save a complex network of objects in a permanent binary form ..."

You are attempting to use CArchive to save a TEXT file as TEXT.  CArchive is opening the output file in "BINARY" mode and so all data being sent to it is "RAW".  You want "\n" to NEWLINE conversion in effect so you should use CStdioFile() instead OR do it manually youself.
0
 

Author Comment

by:codechewer
ID: 9671625
well thanks AlexFm and jhance. i guess i wasn't confortable with changing to CStdioFile as i had used CArchive more often. i used the code snippet

   char buf[] = {0x0d, 0x0a};
     arStore.Write(buf,2);
and it worked just fine. thanks alot.

u guys are my heroes. is there anyway to share the points i really want to know. else jhance gets all the points.
0
 
LVL 32

Expert Comment

by:jhance
ID: 9671630
Yes, you can SPLIT the points.  Glad to help.
0
 

Author Comment

by:codechewer
ID: 9671655
how? please
0
 
LVL 44

Expert Comment

by:AndyAinscow
ID: 9671673
You could also just use "\r\n" instead of "\n"
0
 
LVL 32

Expert Comment

by:jhance
ID: 9671690
Hmmm, that's a good question.  It's not obvious to me at this point and I don't ask that many questions....

Perhaps AlexFM knows how...
0
 

Author Comment

by:codechewer
ID: 9671744
i found how. it was right before me. i guess i was looking out to a more complex procedure or i wasn't meticulus enough to address the problem of sharing points. whatever it was.. thanks very much. :)
0

Featured Post

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Decoding 32 bit binary streams 6 49
ODBC Connection Logging, ADO.NET 6 69
Problem to event 3 104
Message not shown 5 67
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: 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…
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

828 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