简体   繁体   中英

Extracting variables from text file (C++)

I need to input four variables into a C++ program (Win32) from a text file. Each of the variables are on their own line.

example.txt:

2.651127
-7.802776
5.530998
-17.239931

These four variables will each be a float in my program. What is the syntax in C++ to pass each line to its own variable? There are only ever four numbers, each on their own line.

#include <fstream>
std::ifstream file("test.txt");
double a, b, c, d;
if(!(file >> a >> b >> c >> d)){
// error extracting the values!
}
double d[4];
for (int i = 0; i < 4; i++)
  cin >> d[i];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM