Solved

C#: WriteByte New Line ???

Posted on 2004-09-24
7
1,672 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 19

Expert Comment

by:drichards
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Your the expert, thank you very much.......
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Suggested Solutions

This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now