Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

writing text to a new line.

Posted on 2003-11-03
11
Medium Priority
?
701 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 80 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
Industry Leaders: 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 120 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 45

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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

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…
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…
Suggested Courses

636 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