Solved

writing text to a new line.

Posted on 2003-11-03
11
673 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Perl Awk Need Help 3 117
Complete beginner needs help making a cron job 9 116
twoTwo  challenge 35 102
wordmultiple challenge 12 132
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

770 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