How can I convert a text file consisting of floating-point numbers into a numeric array. For example, there is a * .txt file. Each column must be a separate array, so that later you can perform various mathematical operations with this array.

enter image description here

Closed due to the fact that off-topic participants αλεχολυτ , AK , user194374, Ruslan_K , ermak0ff Jan 29 '17 at 8:28 .

  • Most likely, this question does not correspond to the subject of Stack Overflow in Russian, according to the rules described in the certificate .
If the question can be reformulated according to the rules set out in the certificate , edit it .

  • 6
    It is not customary to do homework for others. - αλεχολυτ

1 answer 1

Well, if there is a comma - for example, like this:

int main(int argc, const char * argv[]) { vector<double> x, y; ifstream in("data"); string s; while(getline(in,s)) { size_t pos; while((pos = s.find(',')) != s.npos) s.replace(pos,1,"."); double a, b; istringstream ss(s); if (ss >> a >> b) { x.push_back(a); y.push_back(b); cout << a << " " << b << endl; } } } 
  • Thanks Harry, the program works, but it displays the numbers in the first column not like in the picture I attached, but appends e-6. Please tell me how to fix? Example: it prints -3,486e-6 and should be -0.000003486 - Bogdan Kit
  • Well, if this is so important, write instead of cout << a - cout << fixed << setprecision(12) << a - these are pure output problems ... - Harry
  • thanks, everything works!) - Bogdan Kit
  • I understand that x and y are vectors. x.push_back (a) here the goal is written to the vector x y.push_back (b) here the value would be written to a vector I understand correctly? Can I then write the data from the vector x to the array t [], and the data from the vector y to write to the array V []? I need the first column to be in the t [] array, and the second in the V [] array. I think that for this I need a for loop. First, using the for loop, I will write data from vector x to the array t [] and then, using the second one, I will write the same cycle from the vector y to the array V []. - Bogdan Kit
  • Well, vectors are the same arrays in C ++. You refer to the data vector<double> x in the same way - as to x[i] . If we really need arrays, write directly to the array, bypassing the vector. But writing in a vector, then later in an array is like ... well, I don’t know ... go for a Mercedes to change it to Zaporozhets ... - Harry