Coffeehouse Post

Single Post Permalink

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

     I'm kind of new to C# (come from a C++ background), and I've got a question regarding file reading. Is there a way to read a large number of ints or doubles from a file directly (as easy as in C++)?

    I've looked into StreamReader, StringReader and BinaryReader and while I can get it done, I think I must be missing something since it's not nearly as straightforward as it is in C++. So far I've used StreamReader to read the entire file, then use ' ' as a delimiter and get my ints that way. 

    Basically I'm looking to do the C# equivalent of this:

    std::ifstream ins;
    ins.open(file_name.c_str());
    if(ins.is_open())
    {
         while(ins)
         {
              ins >> tmp;
              arr[iter] = tmp; iter++;
          }
    }