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
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);
}
}
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