Coffeehouse Post

Single Post Permalink

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

    By the way, one of the neat things of the STL is that you can actually write the OP's code like this:

    std::ifstream input(fileName.c_str());
    std::vector<int> values;
    if( input )
        std::copy(std::istream_iterator<int>(input), std::istream_iterator<int>(), std::back_inserter(values));

    The C# equivalent of that would be to use LINQ, I guess:

    var values = from line in File.ReadLines(fileName)
                 from value in line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries)
                 select Convert.ToInt32(value);