Solved

C#: WriteByte New Line ???

Posted on 2004-09-24
7
1,720 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
  • 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
U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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