Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

C#: WriteByte New Line ???

Posted on 2004-09-24
7
Medium Priority
?
1,827 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
Independent Software Vendors: 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!

 
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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
Suggested Courses

730 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