473,385 Members | 1,740 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

StringWriter to Text file

I am trying to write
'StringWriter SW = new StringWriter();' to a FileStream.
The code writes the text to file, but NotePad does not see the NewLines.

Below is the code snippet taken from a large project

////////////// Main ////////////////////////////////////////
StringWriter DataWriter ;

this.FileOut = new FileStream( @"c:\RegChk.txt" ,FileMode.Create );
this.BinaryOut = new BinaryWriter( this.FileOut );
-------------------------------------------------------------------------
StringWriter SW = new StringWriter();
this.SetWriter(SW);
-------------------------------------------------------------------------
SW.Close();

StringBuilder bu = SW.GetStringBuilder();
string dataSW = bu.ToString() ;

// print only of there is data
if ( dataSW.Length.Equals(0).Equals(false) )
{
string rKeyString = "Registry Key '"+rkeyName+"'\n";
this.BinaryOut.Write( rKeyString);
this.BinaryOut.Write(dataSW);
this.BinaryOut.Write("\n");
this.BinaryOut.Flush();
}
================================================== ==================================

public void SetWriter( StringWriter dataOut )
{
try
{
if ( ! dataOut.Equals(null) )
{
this.DataWriter = dataOut;
}
}
catch( System.NullReferenceException )
{
// error
}
}
private void PrintData( string msg , string key , string data )
{
this.DataWriter.Write( msg );
this.DataWriter.Write( " '");
this.DataWriter.Write( key );
this.DataWriter.Write( "' '");
this.DataWriter.Write( data );
this.DataWriter.Write( "\n");
}
Nov 15 '05 #1
3 20541
Robert Linder wrote:
The code writes the text to file, but NotePad does not see the
NewLines.
[...]
this.BinaryOut.Write("\n");
[...]
this.DataWriter.Write( "\n");


Notepad does NOT use "\n" as line sparator!
Instead use: "\r\n" !

--
Greetings
Jochen

Do you need a memory-leak finder ?
http://www.codeproject.com/tools/leakfinder.asp
Nov 15 '05 #2
Robert,

Don't write "\n". Try using Environment.NewLine, and that should work.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Robert Linder" <rl*****@panix.com> wrote in message
news:c2**********@reader2.panix.com...
I am trying to write
'StringWriter SW = new StringWriter();' to a FileStream.
The code writes the text to file, but NotePad does not see the NewLines.

Below is the code snippet taken from a large project

////////////// Main ////////////////////////////////////////
StringWriter DataWriter ;

this.FileOut = new FileStream( @"c:\RegChk.txt" ,FileMode.Create );
this.BinaryOut = new BinaryWriter( this.FileOut );
-------------------------------------------------------------------------
StringWriter SW = new StringWriter();
this.SetWriter(SW);
-------------------------------------------------------------------------
SW.Close();

StringBuilder bu = SW.GetStringBuilder();
string dataSW = bu.ToString() ;

// print only of there is data
if ( dataSW.Length.Equals(0).Equals(false) )
{
string rKeyString = "Registry Key '"+rkeyName+"'\n";
this.BinaryOut.Write( rKeyString);
this.BinaryOut.Write(dataSW);
this.BinaryOut.Write("\n");
this.BinaryOut.Flush();
}
================================================== ==========================
========
public void SetWriter( StringWriter dataOut )
{
try
{
if ( ! dataOut.Equals(null) )
{
this.DataWriter = dataOut;
}
}
catch( System.NullReferenceException )
{
// error
}
}
private void PrintData( string msg , string key , string data )
{
this.DataWriter.Write( msg );
this.DataWriter.Write( " '");
this.DataWriter.Write( key );
this.DataWriter.Write( "' '");
this.DataWriter.Write( data );
this.DataWriter.Write( "\n");
}

Nov 15 '05 #3

The Solution:
I added 'this.NL = Environment.NewLine;' to my code and replaced
Write("\n") with Write(this.NL)

Output is looks good in Notepad and gvim.

Thanks

Robert Linder wrote:
I am trying to write
'StringWriter SW = new StringWriter();' to a FileStream.
The code writes the text to file, but NotePad does not see the NewLines.

Below is the code snippet taken from a large project

////////////// Main ////////////////////////////////////////
StringWriter DataWriter ;

this.FileOut = new FileStream( @"c:\RegChk.txt" ,FileMode.Create );
this.BinaryOut = new BinaryWriter( this.FileOut );
-------------------------------------------------------------------------
StringWriter SW = new StringWriter();
this.SetWriter(SW);
-------------------------------------------------------------------------
SW.Close();

StringBuilder bu = SW.GetStringBuilder();
string dataSW = bu.ToString() ;

// print only of there is data
if ( dataSW.Length.Equals(0).Equals(false) )
{
string rKeyString = "Registry Key '"+rkeyName+"'\n";
this.BinaryOut.Write( rKeyString);
this.BinaryOut.Write(dataSW);
this.BinaryOut.Write("\n");
this.BinaryOut.Flush();
}
================================================== ==================================
public void SetWriter( StringWriter dataOut )
{
try
{
if ( ! dataOut.Equals(null) )
{
this.DataWriter = dataOut;
}
}
catch( System.NullReferenceException )
{
// error
}
}
private void PrintData( string msg , string key , string data )
{
this.DataWriter.Write( msg );
this.DataWriter.Write( " '");
this.DataWriter.Write( key );
this.DataWriter.Write( "' '");
this.DataWriter.Write( data );
this.DataWriter.Write( "\n");
}

Nov 15 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Rigga | last post by:
Hi, I am new to Python and need to parse a text file and cut parts out i.e. say the text file contained 5 rows of text: line 1 of the text file line 2 of the text file line 3 of the text...
2
by: footballhead | last post by:
I have a page with a datagrid that I have configured with all the information I need to provide to froogle for listing my products on their site. I am having trouble finding a way to successfully...
4
by: AHP | last post by:
Hi, I'm using Visual Studio 2005. I am developing a web application that uses the FileUpload control to upload text files to a directory on a webserver. This works fine. However, for me to be...
4
by: Andyza | last post by:
I'm using FileSystemObject to open and write to a tab delimited text file. First, I connect to a database and select some data. Then I create the text file and insert each record in the text...
3
by: R. P. | last post by:
Subject: XSLT to transform a flat XML file into a structured text file I have an XML file that lists the PDF file segment names and titles of a larger document and looks something like this: ...
1
by: Osoccer | last post by:
...to a different folder and in the relocated file concatenates all of the lines in one long string with a space between each line element. Here is a fuller statement of the problem: I need a...
2
by: nuhura01 | last post by:
Hi.. I'm trying to save query path which is in text box to text file. Below is the coding that i'm using currently: Private Sub SaveQueryPath() 'save to text file Response.Clear()...
2
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I'm using a windows form here,and I need to convert a datagridview (actually its an Inragistics UltraGridView but whatever) to a text file. I want each row to be in a single line. I found...
6
Atran
by: Atran | last post by:
Hello: In this article: You will learn to Write or Read A Text File. Let's Begin: First Create a new project (ConsoleApp or WinApp). And Make sure your program uses these namespaces: using...
10
by: bluemountain | last post by:
Hi there, Iam new to python forms and programming too I had a text file where i need to extract few words of data from the header(which is of 3 lines) and search for the keyword TEXT1, TEXT2,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.