473,394 Members | 1,735 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to file-read ASCIIZ (null-terminated strings)?

I need to read a binary file which has mixed data types embedded in it.
Fixed length strings, 16-bit integers and zero (null) terminated ASCII
strings. (akak ASCIIZ)

The first two types are no problem, but the zero terminated string is a
problem because I have to read byte--by-byte with BinaryStream(), and I
don't know whether the class or the O/S uses buffering to make this
efficient. Is my code slowing the app down - is there a better class or
technique other than reading in byte-by-byte and checking for char 0?

(Code below)

Thanks,

Carlo

/// <summary>
/// Read an AsciiZ string in from the binary reader
/// </summary>
/// <param name="dicReader">Binary reader instance</param>
/// <returns>String, null terminator is truncted,
/// stream reader positioned at byte after null</returns>
public static string ReadStringZ(BinaryReader reader) {
string result = "";
char c;
for (int i = 0; i < reader.BaseStream.Length; i++) {
if ((c = (char) reader.ReadByte()) == 0) {
break;
}
result += c.ToString();
}
return result;
}
Feb 27 '06 #1
3 5602
Is my code slowing the app down


You tell us! Are you having performance problems that you think are
related to this procedure? If not, I would't worry.

If you want to improve something, I would try replacing the string
concatenation (which creates a new string object for each iteration)
with a StringBuilder.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 27 '06 #2
Thanks for the StringBuilder tip - I will use it, as well as in other cases
where I am concatenating strings. I'm at the beginning of a project to
duplicate the functionility of an extensive database dictionary originally
written in Clipper for DOS, so I am trying to implement best practices early
on.

Does that mean that it's KNOWN that BinaryReader uses buffering that makes
byte-by-byte reads efficient? I don't know much about the internals of
stream readers.

Thanks for the reply,

Carlo
"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OA***************@tk2msftngp13.phx.gbl...
Is my code slowing the app down


You tell us! Are you having performance problems that you think are
related to this procedure? If not, I would't worry.

If you want to improve something, I would try replacing the string
concatenation (which creates a new string object for each iteration)
with a StringBuilder.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Feb 28 '06 #3
>Does that mean that it's KNOWN that BinaryReader uses buffering that makes
byte-by-byte reads efficient? I don't know much about the internals of
stream readers.


I wouldn't assume that, those kind of things are implementation
dependent. But I know that at least on the desktop .NET framework, the
underlying FileStream does buffering.
Mattias

--
Mattias Sjögren [C# MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Feb 28 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

20
by: Ravi | last post by:
Hi, I have about 200GB of data that I need to go through and extract the common first part of a line. Something like this. >>>a = "abcdefghijklmnopqrstuvwxyz" >>>b = "abcdefghijklmnopBHLHT"...
17
by: Gordon Airport | last post by:
Has anyone suggested introducing a mutable string type (yes, of course) and distinguishing them from standard strings by the quote type - single or double? As far as I know ' and " are currently...
2
by: Sergey Dorofeev | last post by:
I can use string.unpack if string in struct uses fixed amount of bytes. But is there some extension to struct modue, which allows to unpack zero-terminated string, size of which is unknown? E.g....
89
by: scroopy | last post by:
Hi, I've always used std::string but I'm having to use a 3rd party library that returns const char*s. Given: char* pString1 = "Blah "; const char* pString2 = "Blah Blah"; How do I append...
14
by: Ben | last post by:
I have an int variable (always <100) that I want to convert to a two character string, e.g. if myint = 1, mystr = "01" if myint = 81, mystr = "81" At the moment I can't figure out how to do...
2
by: Potiuper | last post by:
Question: Is it possible to use a char pointer array ( char *<name> ) to read an array of strings from a file in C? Given: code is written in ANSI C; I know the exact nature of the strings to be...
74
by: cman | last post by:
Can you "walk across" C strings or char pointers (using *(sz+1)) like you can with arrays. If not, why not? If so, how? cman
95
by: hstagni | last post by:
Where can I find a library to created text-based windows applications? Im looking for a library that can make windows and buttons inside console.. Many old apps were make like this, i guess ...
1
by: monkey0525 | last post by:
I'm writing a program that reads information from three seperate classes. Here is my code: public class Animal { protected int id; protected String type; protected double mass; ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.