Solved

Write a string to a file

Posted on 2004-08-18
2
263 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
[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
2 Comments
 
LVL 10

Accepted Solution

by:
eternal_21 earned 125 total points
ID: 11834348
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
ID: 11853782
Thx, eternal_21. Very nice solution...
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

734 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