Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Read From file data till reach ‘\0’

Read From file data till reach ‘\0’

Scheduled Pinned Locked Moved C#
questioncsharpdata-structures
5 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    Ronenb
    wrote on last edited by
    #1

    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

    L P 3 Replies Last reply
    0
    • R Ronenb

      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

      L Offline
      L Offline
      Lev Danielyan
      wrote on last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Ronenb

        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

        P Online
        P Online
        PIEBALDconsult
        wrote on last edited by
        #3

        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's ReadString 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

        1 Reply Last reply
        0
        • R Ronenb

          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

          P Online
          P Online
          PIEBALDconsult
          wrote on last edited by
          #4

          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.

          R 1 Reply Last reply
          0
          • P PIEBALDconsult

            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.

            R Offline
            R Offline
            Ronenb
            wrote on last edited by
            #5

            Thanks you all Ronen

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups