Solved

Write a string to a file

Posted on 2004-08-18
2
231 Views
Last Modified: 2010-04-15
Hello,
I have strings of different length to be written to a flat file. The block to be written to file (containing single string) must contain 255 characters. (in order to write in a kind of a structure to a file). Is there a function to write such blocks WITHOUT having to "fill up" every string to 255-character size? If not, what could be the fastest solution?
Thx in advance!
0
Comment
Question by:zaratin
2 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 125 total points
Comment Utility
Assuming you are saving using a FileStream, the following method would work (truncates strings longer than 255 characters):

  static void WriteString(System.IO.FileStream fileStream, string outString)
  {
    byte[] textBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(outString);
    byte[] bytes = new byte[(textBytes.Length > 255 ? (int)textBytes.Length : 255)];
    textBytes.CopyTo(bytes, 0);
    fileStream.Write(bytes, 0, 255);
  }

(throws exception if string is longer than 255 characters)

  static void WriteString(System.IO.FileStream fileStream, string outString)
  {
    byte[] bytes = new byte[255];
    System.Text.ASCIIEncoding.ASCII.GetBytes(outString).CopyTo(bytes, 0);
    fileStream.Write(bytes, 0, 255);
  }
0
 

Author Comment

by:zaratin
Comment Utility
Thx, eternal_21. Very nice solution...
0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

762 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

7 Experts available now in Live!

Get 1:1 Help Now