Solved

Write a string to a file

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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

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…
Summary: Persistence is the capability of an application to store the state of objects and recover it when necessary. This article compares the two common types of serialization in aspects of data access, readability, and runtime cost. A ready-to…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

729 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