Solved

writing text to a new line.

Posted on 2003-11-03
11
654 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
 
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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
repeateFront java challenge 31 87
Prevent this page from creating additional dialogs. 3 264
wordappend challenge 8 86
Change to event 1 74
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

747 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

14 Experts available now in Live!

Get 1:1 Help Now