Coffeehouse Post

Single Post Permalink

View Thread: Reading int from file in C#
  • User profile image
    Sven Groot

    Alright, because I was bored, here's a class that extracts strings from a file on whitespace boundaries, in a similar way that C++ iostreams do.

    class WordReader : IDisposable
    {
        private StreamReader _reader;
        private char[] _buffer = new char[1024];
        private int _bufferPos;
        private int _bufferSize;
    
        public WordReader(string file)
        {
            _reader = File.OpenText(file);
        }
    
        public string ReadWord()
        {
            // Possible optimization: reuse the StringBuilder
            StringBuilder result = new StringBuilder(80);
            // Skip whitespace
            while( (_bufferPos < _bufferSize || FillBuffer()) && char.IsWhiteSpace(_buffer[_bufferPos]) )
            {
                ++_bufferPos;
            }
    
            // Read word
            while( (_bufferPos < _bufferSize || FillBuffer()) && !char.IsWhiteSpace(_buffer[_bufferPos]) )
            {
                result.Append(_buffer[_bufferPos]);
                ++_bufferPos;
            }
    
            if( result.Length == 0 )
                return null;
            else
                return result.ToString();
        }
    
        public IEnumerable<string> ReadWords()
        {
            string value;
            while( (value = ReadWord()) != null )
                yield return value;
        }
    
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }
    
        protected virtual void Dispose(bool disposing)
        {
            if( disposing )
                _reader.Dispose();
        }
    
        private bool FillBuffer()
        {
            _bufferSize = _reader.Read(_buffer, 0, _buffer.Length);
            _bufferPos = 0;
            return _bufferSize > 0;
        }
    }
    

    Which can then be used like this:

    int[] values;
    using( WordReader reader = new WordReader(fileName) )
    {
        values = reader.ReadWords().Select(word => Convert.ToInt32(word)).ToArray();
    }
    

    Big Smile