CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2011
    Posts
    4

    reading a text file table into 2 double arrays

    hi
    i'm very new to this ( i've been a matlab user for the past 6 yrs so it's very hard for me...)
    i need to read a text file looking like:

    alpha beta
    1 2
    3 4

    and i need to get an array for the firt col and the 2nd col i don't know the length of the data
    plz help

  2. #2
    Join Date
    Aug 2000
    Location
    West Virginia
    Posts
    7,721

    Re: reading a text file table into 2 double arrays

    The basic idea would be to use a std::vector, which grows in size automatically.

    Here is a small example. Note: instead of two vectors, it would be better to
    create a class with two data members and have one vector of that class.

    Code:
    #include <vector>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      vector<int> field1;
      vector<int> field2;
      
      int f1,f2;
      
      ifstream in("your_data.txt");
      
      in.ignore(1000,'\n');  // skip header line
      
      while (in >> f1 >> f2)
      {
        field1.push_back(f1);
        field2.push_back(f2);
      }
      
      
    }

  3. #3
    Join Date
    Feb 2011
    Location
    United States
    Posts
    1,016

    Re: reading a text file table into 2 double arrays

    Note to others reading this thread; see also the C# thread asking the same question: http://www.codeguru.com/forum/showthread.php?t=513577
    Best Regards,

    BioPhysEngr
    http://blog.biophysengr.net
    --
    All advice is offered in good faith only. You are ultimately responsible for effects of your programs and the integrity of the machines they run on.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured