Read From file data till reach ‘\0’
-
Hi I need to convert c code to C#, I need to open a file, read strings and init local members. The file is composed of strings and ‘\0’ between each string How can I read string while the indication of end of string is ‘\0’? The c code is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; fread( (char*)& comment, sizeof(comment),1,file); C# code that I Try is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; comment = m_brSWL.ReadBytes(MAX_STR); but the array is not init with the string I also try readChar but it read also the second string , not till uit reach ‘\0’ Any Idea? Thanks Ronen
-
Hi I need to convert c code to C#, I need to open a file, read strings and init local members. The file is composed of strings and ‘\0’ between each string How can I read string while the indication of end of string is ‘\0’? The c code is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; fread( (char*)& comment, sizeof(comment),1,file); C# code that I Try is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; comment = m_brSWL.ReadBytes(MAX_STR); but the array is not init with the string I also try readChar but it read also the second string , not till uit reach ‘\0’ Any Idea? Thanks Ronen
I don't have such a file at hand, but try this:
using (StreamReader sr = new StreamReader("file")) {
string content = sr.ReadToEnd();
string[] strings = content.Split('\0');
}Regards, Lev
-
Hi I need to convert c code to C#, I need to open a file, read strings and init local members. The file is composed of strings and ‘\0’ between each string How can I read string while the indication of end of string is ‘\0’? The c code is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; fread( (char*)& comment, sizeof(comment),1,file); C# code that I Try is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; comment = m_brSWL.ReadBytes(MAX_STR); but the array is not init with the string I also try readChar but it read also the second string , not till uit reach ‘\0’ Any Idea? Thanks Ronen
It appears that you are trying to read the file as binary (the file was created by a legacy system?), so try
the System.IO.BinaryReader
'sReadString
method, I haven't tried it (yet), but I suspect it may work. [Edit: I've tried it, it expects the new kind of string, not a null-terminated string. :( ] On the other hand, I'd more likely read in each byte separately.modified on Wednesday, December 31, 2008 11:15 AM
-
Hi I need to convert c code to C#, I need to open a file, read strings and init local members. The file is composed of strings and ‘\0’ between each string How can I read string while the indication of end of string is ‘\0’? The c code is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; fread( (char*)& comment, sizeof(comment),1,file); C# code that I Try is int MAX_STR = 200; byte[] comment = new byte[MAX_STR]; comment = m_brSWL.ReadBytes(MAX_STR); but the array is not init with the string I also try readChar but it read also the second string , not till uit reach ‘\0’ Any Idea? Thanks Ronen
Here's a fairly simple method:
public static string ReadString ( System.IO.Stream Stream , byte Terminator ) { string result = null ; System.Collections.Generic.List<byte> bytes = new System.Collections.Generic.List<byte>() ; int temp ; while ( true ) { temp = Stream.ReadByte() ; if ( ( temp == Terminator ) || ( temp == -1 )) { break ; } bytes.Add ( (byte) temp ) ; } if ( bytes.Count > 0 ) { result = System.Text.Encoding.ASCII.GetString ( bytes.ToArray() ) ; } else { if ( temp == Terminator ) { result = "" ; } } return ( result ) ; }
You can convert it to an Extension Method if you like.
-
Here's a fairly simple method:
public static string ReadString ( System.IO.Stream Stream , byte Terminator ) { string result = null ; System.Collections.Generic.List<byte> bytes = new System.Collections.Generic.List<byte>() ; int temp ; while ( true ) { temp = Stream.ReadByte() ; if ( ( temp == Terminator ) || ( temp == -1 )) { break ; } bytes.Add ( (byte) temp ) ; } if ( bytes.Count > 0 ) { result = System.Text.Encoding.ASCII.GetString ( bytes.ToArray() ) ; } else { if ( temp == Terminator ) { result = "" ; } } return ( result ) ; }
You can convert it to an Extension Method if you like.