473,799 Members | 3,382 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Null Terminated ASCII String from a File Stream.

I need to read a null terminated ASCII string from a FileStream. How can I
do that? I know that with FileStream.Read Byte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a
way to do this easier?

Nov 16 '05 #1
3 8583
Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class (found
in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Michel Racicot" <mi************ *********@cgi.c om> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
I need to read a null terminated ASCII string from a FileStream. How can I
do that? I know that with FileStream.Read Byte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a
way to do this easier?

Nov 16 '05 #2
Kind of, thank you for the Info... But the file is binary (but includes null
terminated strings at certain offsets).

So far, I use the BinaryReader class to find the offsets of the strings, I
wanted to know if I could also use BinaryReader to read the strings
themselves.

Any idea?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Oj******** ******@TK2MSFTN GP11.phx.gbl...
Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class (found in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Michel Racicot" <mi************ *********@cgi.c om> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
I need to read a null terminated ASCII string from a FileStream. How can I do that? I know that with FileStream.Read Byte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a way to do this easier?


Nov 16 '05 #3


"Michel Racicot" wrote:
Kind of, thank you for the Info... But the file is binary (but includes null
terminated strings at certain offsets).

So far, I use the BinaryReader class to find the offsets of the strings, I
wanted to know if I could also use BinaryReader to read the strings
themselves.
you use the BinaryReader to read in a byte[], then pass that to the
appropriate System.Text.Enc oding class to convert to a string.

Any idea?

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard .caspershouse.c om> wrote in
message news:Oj******** ******@TK2MSFTN GP11.phx.gbl...
Michel,

I would take the FileStream and pass it to a StreamReader, along with
the return value from the static property ASCII on the Encoding class

(found
in System.Text). Once you have that, you can just call ReadLine on the
stream reader, and it will allow you to read the contents line by line.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Michel Racicot" <mi************ *********@cgi.c om> wrote in message
news:el******** ******@TK2MSFTN GP10.phx.gbl...
I need to read a null terminated ASCII string from a FileStream. How can I do that? I know that with FileStream.Read Byte, I can read until I get a
byte == 0 but how can I convert a single byte to a character? Is there a way to do this easier?



Nov 16 '05 #4

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

Similar topics

35
9995
by: David Mathog | last post by:
Every so often one of my fgets() based programs encounters an input file containing embedded nulls. fgets is happy to read these but the embedded nulls subsequently cause problems elsewhere in the program. Since fgets() doesn't return the number of characters read it is pretty tough to handle the embedded nulls once they are in the buffer. So two questions: 1. Why did the folks who wrote fgets() have a successful
5
24730
by: David Sworder | last post by:
Hi, I've created a UserControl-derived class called MyUserControl that is able to persist and subsequently reload its state. It exposes two methods as follows: public void Serialize(Stream s); public void Deserialize(Stream s); Within the MyUserControl class, there is a field of type MyInnerClass
2
6205
by: Martín Marconcini | last post by:
Hello there, I'm writting (or trying to) a Console Application in C#. I has to be console. I remember back in the old days of Cobol (Unisys), Clipper and even Basic, I used to use a program (its name i cannot recall now...) where I designed the "screen" using this "program" and then saved it into an ASCII file. (thus, using 'extended' ASCII's like Lines, Corners, etc. and making screens look nicer and more professional). Then reading a...
3
5888
by: Asha | last post by:
greetings, i have some questions below, what are the differences between private string _strVal = string.Empty; and _strVal = null; does the string.Empty; allocate memory for it? how about _strVal = "" thanks for enlightening me on this.
18
34150
by: Ger | last post by:
I have not been able to find a simple, straight forward Unicode to ASCII string conversion function in VB.Net. Is that because such a function does not exists or do I overlook it? I found Encoding.Convert, but that needs byte arrays. Thanks, /Ger
3
5636
by: Carlo Stonebanks | last post by:
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...
12
1985
by: semut | last post by:
Given that the string is of null terminated type. What could be the possible causes (by experience) the string to have no null terminated and cause buffer overflow later. I know it is quite broad, just like to find out the causes as much as possible so that I could impose stricter checking toward my codes. note: I could not use std::string cause it will require a total rewrite. thanks.
14
3027
by: mast2as | last post by:
Hi everyone, I am trying to implement some specs which specify that an array of parameter is passed to a function as a pointer to an array terminated by a NULL chatacter. That seemed fairly easy to implement. I had a special Malloc function that would allocated the number of needed bytes for the objects i needed to store + 1 additional byte to save the NULL character /*!
5
1949
by: ssylee | last post by:
If I'm being supplied with a char* that is not null-terminated, is it impossible to transform it into a null terminated char* with only abstract information about the char* information?
0
10259
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10238
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10030
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9077
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6809
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5589
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4145
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3761
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.