Solved

Write a string to a file

Posted on 2004-08-18
2
241 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
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
creating a flowchart from an algorithm 5 30
Data Saving 5 56
Iterate a dictionnary to change values 4 53
C# Comparing String Arrays. 5 20
In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Hi friends,  in this video  I'll show you how new windows 10 user can learn the using of windows 10. Thank you.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

20 Experts available now in Live!

Get 1:1 Help Now