Arbitrary Streamwriter.

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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();
 
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!
***************************
 
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.
 
Arne said:
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?
 
Arne said:
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...
 
Cowboy (Gregory A. Beamer) - MVP said:
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,
Maybe it is my stupid editor that adds another carriage return. What would
be your favorite tool to display all the bytes?
 
Arne said:
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.
 
Back
Top