PC Review


Reply
Thread Tools Rate Thread

Arbitrary Streamwriter.

 
 
=?Utf-8?B?QXJuZQ==?=
Guest
Posts: n/a
 
      11th Apr 2005
The streamwriter appends an extra carriage return at the end. Is there a way
to supress that?

file = @"c:\temp\text.txt";
fs = new FileStream(file,
FileMode.Create, FileAccess.Write, FileShare.None);
StreamWriter sw = new StreamWriter(fs);
sw.Write("BINARY PLEASE");
sw.Flush();
sw.Close();
 
Reply With Quote
 
 
 
 
=?Utf-8?B?Q293Ym95IChHcmVnb3J5IEEuIEJlYW1lcikgLSBN
Guest
Posts: n/a
 
      11th Apr 2005
As I understand, this is normal. Without the carriage return at the end, you
do not end up with the null that indicates EOS.


---

Gregory A. Beamer
MVP; MCP: +I, SE, SD, DBA

***************************
Think Outside the Box!
***************************

"Arne" wrote:

> The streamwriter appends an extra carriage return at the end. Is there a way
> to supress that?
>
> file = @"c:\temp\text.txt";
> fs = new FileStream(file,
> FileMode.Create, FileAccess.Write, FileShare.None);
> StreamWriter sw = new StreamWriter(fs);
> sw.Write("BINARY PLEASE");
> sw.Flush();
> sw.Close();

 
Reply With Quote
 
=?Utf-8?B?QXJuZQ==?=
Guest
Posts: n/a
 
      11th Apr 2005
Cowboy,
It might be normal, but it doesn't fit my business requirement. I need to
create a file without a carriage return that I can FTP to a mainframe, where
carriage returns are not welcome.


"Cowboy (Gregory A. Beamer) - MVP" wrote:

> As I understand, this is normal. Without the carriage return at the end, you
> do not end up with the null that indicates EOS.
>
>
> ---
>
> Gregory A. Beamer
> MVP; MCP: +I, SE, SD, DBA
>
> ***************************
> Think Outside the Box!
> ***************************
>
> "Arne" wrote:
>
> > The streamwriter appends an extra carriage return at the end. Is there a way
> > to supress that?
> >
> > file = @"c:\temp\text.txt";
> > fs = new FileStream(file,
> > FileMode.Create, FileAccess.Write, FileShare.None);
> > StreamWriter sw = new StreamWriter(fs);
> > sw.Write("BINARY PLEASE");
> > sw.Flush();
> > sw.Close();

 
Reply With Quote
 
Jon Shemitz
Guest
Posts: n/a
 
      11th Apr 2005
Arne wrote:

> The streamwriter appends an extra carriage return at the end. Is there a way
> to supress that?
>
> file = @"c:\temp\text.txt";
> fs = new FileStream(file,
> FileMode.Create, FileAccess.Write, FileShare.None);
> StreamWriter sw = new StreamWriter(fs);
> sw.Write("BINARY PLEASE");
> sw.Flush();
> sw.Close();


Does the StreamWriter's NewLine property do what you want?

--

www.midnightbeach.com
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      11th Apr 2005
Arne <(E-Mail Removed)> wrote:
> The streamwriter appends an extra carriage return at the end. Is there a way
> to supress that?
>
> file = @"c:\temp\text.txt";
> fs = new FileStream(file,
> FileMode.Create, FileAccess.Write, FileShare.None);
> StreamWriter sw = new StreamWriter(fs);
> sw.Write("BINARY PLEASE");
> sw.Flush();
> sw.Close();


The above code doesn't write a carriage return. Try running the
following program (containing your code, modified for my directory
structure):

using System;
using System.IO;

class Test
{
static void Main()
{
string file = @"c:\test\text.txt";
FileStream fs = new FileStream(file,
FileMode.Create, FileAccess.Write,
FileShare.None);
StreamWriter sw = new StreamWriter(fs);
sw.Write("BINARY PLEASE");
sw.Flush();
sw.Close();
}
}

The file length afterwards is 13 bytes, which doesn't leave any room
for carriage returns, if you count the characters in the string...

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      11th Apr 2005
Cowboy (Gregory A. Beamer) - MVP <(E-Mail Removed)>
wrote:
> As I understand, this is normal.


No it's not - and it doesn't actually happen. (Look at my other reply
to this thread.)

> Without the carriage return at the end, you
> do not end up with the null that indicates EOS.


There *is* no null that indicates end of stream. Some APIs may return 0
or some other special value to indicate the end of the stream, but it's
not actually present as a logical part of the file. The file itself (as
created by the OP's code) is 13 bytes long - one byte per character (as
all the characters are ASCII, and StreamWriter uses UTF-8 by default).

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
=?Utf-8?B?QXJuZQ==?=
Guest
Posts: n/a
 
      12th Apr 2005
Jon,
Maybe it is my stupid editor that adds another carriage return. What would
be your favorite tool to display all the bytes?


"Jon Skeet [C# MVP]" wrote:

> Arne <(E-Mail Removed)> wrote:
> > The streamwriter appends an extra carriage return at the end. Is there a way
> > to supress that?
> >
> > file = @"c:\temp\text.txt";
> > fs = new FileStream(file,
> > FileMode.Create, FileAccess.Write, FileShare.None);
> > StreamWriter sw = new StreamWriter(fs);
> > sw.Write("BINARY PLEASE");
> > sw.Flush();
> > sw.Close();

>
> The above code doesn't write a carriage return. Try running the
> following program (containing your code, modified for my directory
> structure):
>
> using System;
> using System.IO;
>
> class Test
> {
> static void Main()
> {
> string file = @"c:\test\text.txt";
> FileStream fs = new FileStream(file,
> FileMode.Create, FileAccess.Write,
> FileShare.None);
> StreamWriter sw = new StreamWriter(fs);
> sw.Write("BINARY PLEASE");
> sw.Flush();
> sw.Close();
> }
> }
>
> The file length afterwards is 13 bytes, which doesn't leave any room
> for carriage returns, if you count the characters in the string...
>
> --
> Jon Skeet - <(E-Mail Removed)>
> http://www.pobox.com/~skeet
> If replying to the group, please do not mail me too
>

 
Reply With Quote
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      12th Apr 2005
Arne <(E-Mail Removed)> wrote:
> Maybe it is my stupid editor that adds another carriage return. What would
> be your favorite tool to display all the bytes?


Either dump (can't remember where I got mine) or frhed (a binary file
editor). Alternatively you could open it in Visual Studio in binary
mode.

--
Jon Skeet - <(E-Mail Removed)>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
difference in Filestream + StreamWriter and just StreamWriter =?Utf-8?B?aXdkdTE1?= Microsoft VB .NET 1 2nd Aug 2006 01:40 PM
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces Daniel Microsoft Dot NET 3 8th Sep 2005 06:47 PM
if System.IO.StreamWriter write throws an exception, is there anyway to close the System.IO.StreamWriter object? it seems to stay open when this happens then future attempts to write to that same path fail because it says its in use by another proces Daniel Microsoft C# .NET 3 8th Sep 2005 03:07 PM
ASP.NET C# WriteToFile StreamWriter error: 'StreamWriter' could not be found rex64 Microsoft C# .NET 4 15th Jun 2005 03:46 PM
capture using(StreamWriter wr = new StreamWriter()) exceptional kids_pro Microsoft C# .NET 6 3rd Sep 2004 07:53 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 02:49 PM.