-
June 10th, 2011, 10:24 AM
#1
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
-
June 10th, 2011, 10:44 AM
#2
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);
}
}
-
June 16th, 2011, 01:23 AM
#3
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|