Fast and efficient way to read a space separated file of numbers into an array?

后端 未结 7 787
情话喂你
情话喂你 2021-01-21 03:42

I need a fast and efficient method to read a space separated file with numbers into an array. The files are formatted this way:

4 6
1 2 3 4 5 6
2 5 4 3 21111 101         


        
7条回答
  •  我在风中等你
    2021-01-21 04:19

    How about:

        static void Main()
        {
            // sample data
            File.WriteAllText("my.data", @"4 6
    1 2 3 4 5 6
    2 5 4 3 21111 101
    3 5 6234 1 2 3
    4 2 33434 4 5 6");
    
            using (Stream s = new BufferedStream(File.OpenRead("my.data")))
            {
                int rows = ReadInt32(s), cols = ReadInt32(s);
                int[,] arr = new int[rows, cols];
                for(int y = 0 ; y < rows ; y++)
                    for (int x = 0; x < cols; x++)
                    {
                        arr[y, x] = ReadInt32(s);
                    }
            }
        }
    
        private static int ReadInt32(Stream s)
        { // edited to improve handling of multiple spaces etc
            int b;
            // skip any preceeding
            while ((b = s.ReadByte()) >= 0 && (b < '0' || b > '9')) {  }
            if (b < 0) throw new EndOfStreamException();
    
            int result = b - '0';
            while ((b = s.ReadByte()) >= '0' && b <= '9')
            {
                result = result * 10 + (b - '0');
            }
            return result;
        }
    

    Actually, this isn't very specific about the delimiters - it'll pretty much assume that anything that isn't an integer is a delimiter, and it only supports ASCII (you use use a reader if you need other encodings).

提交回复
热议问题