用c++读取以下内容的文件,将其逐个存放于一个数列中“ 512.00 780.00 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 50.0 0.012”
时间: 2024-05-14 09:19:14 浏览: 55
假设文件名为data.txt,文件内容为:
```
512.00 780.00 1.0 0.0 0.0 1.0 1.0 0.0 0.0 1.0 50.0 0.012
```
下面是一个读取文件并将其存放到数组中的示例代码:
```cpp
#include <iostream>
#include <fstream>
#include <vector>
int main() {
std::ifstream infile("data.txt");
if (!infile.is_open()) {
std::cout << "Failed to open file!" << std::endl;
return 1;
}
std::vector<double> nums;
double num;
while (infile >> num) {
nums.push_back(num);
}
infile.close();
std::cout << "Read " << nums.size() << " numbers from file." << std::endl;
for (double n : nums) {
std::cout << n << " ";
}
return 0;
}
```
输出结果为:
```
Read 12 numbers from file.
512 780 1 0 0 1 1 0 0 1 50 0.012
```
阅读全文