Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

C#: WriteByte New Line ???

Posted on 2004-09-24
7
Medium Priority
?
1,873 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
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.

 
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's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

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

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
Hello there! As a developer I have modified and refactored the unit tests which was written by fellow developers in the past. On the course, I have gone through various misconceptions and technical challenges when it comes to implementation. I would…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…

877 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