Solved

C#: WriteByte New Line ???

Posted on 2004-09-24
7
1,792 Views
Last Modified: 2012-08-13
I'm writing bytes to a text file and currently experiencing continuous lines. I've searched the Internet and MSDN and cannot find a method that works correctly.....

Q. How can I write each byte on a new line ???

Current Code:
fs = new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
foreach (byte bNext in sendBytes)
{
fs.WriteByte(bNext);
}
fs.Close();


Alternate Code:
fs = new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
for(int i = 0; i < str.Length; i++)
{
fs.WriteByte(sendBytes[i]);            
}
fs.Close();
0
Comment
Question by:kvnsdr
[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
  • 3
  • 3
7 Comments
 
LVL 10

Expert Comment

by:eternal_21
ID: 12145136
Do this:

  foreach (byte bNext in sendBytes)
  {
    fs.WriteByte(bNext);
    fs.WriteByte(Convert.ToByte('\r'));
    fs.WriteByte(Convert.ToByte('\n'));
  }
0
 
LVL 10

Expert Comment

by:eternal_21
ID: 12145155
Or even better, this:

  byte[] newLine = System.Text.ASCIIEncoding.ASCII.GetBytes(Environment.NewLine);
  foreach (byte bNext in sendBytes)
  {
    fs.WriteByte(bNext);
    fs.Write(newLine, 0, newLine.Length);
  }
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12145591
Well, both examples break the entire byte into individual characters and writes each on a new line like so:

T
h
i
s

a

s
e
n
t
e
n
c
e

My goal is to provide the following results:

This is a sentence
This is a sentence
0
Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

 
LVL 19

Expert Comment

by:drichards
ID: 12145819
How do you know when the lines end?  If you can figure that out, just use the code already given to add the CRLF pair to the ends of the lines instead of after each character.

If each 'sendbytes' is a line, just output the CRLF after outputting all the bytes.

  byte[] newLine = System.Text.ASCIIEncoding.ASCII.GetBytes(Environment.NewLine);
  foreach (byte bNext in sendBytes)
  {
    fs.WriteByte(bNext);
  }
  fs.Write(newLine, 0, newLine.Length);
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12146139
Probably should have included this in my initial question. This is the sendBytes 'Byte'

Byte[] sendBytes = Encoding.ASCII.GetBytes(machineName + str);                        
0
 
LVL 10

Accepted Solution

by:
eternal_21 earned 500 total points
ID: 12148175
Okay, do this:

  byte[] newLine = System.Text.ASCIIEncoding.ASCII.GetBytes(Environment.NewLine);

  fs = new FileStream("SyslogTest.txt", FileMode.Append, FileAccess.Write, FileShare.Read);
  using(fs)
  {
    Byte[] sendBytes = Encoding.ASCII.GetBytes(machineName + str);
    fs.Write(sendBytes, 0, sendBytes.Length);
    fs.Write(newLine, 0, newLine.Length);
  }
0
 
LVL 1

Author Comment

by:kvnsdr
ID: 12151701
Your the expert, thank you very much.......
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…

617 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