Solved

writing text to a new line.

Posted on 2003-11-03
11
689 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
[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
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
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 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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 50
Separate files macro - Errors on larger data set 18 76
Generic progress indicator 6 121
if loop in java 3 174
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.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

738 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