如何使用fstream从文本中编写一系列浮点数

How to write a series of float from text using fstream

本文关键字:一系列 浮点数 文本 何使用 fstream      更新时间:2023-10-16

示例ABC.txt10.F 30.2f 20 F

我想检索这些信息并存储在我的数组中。然而,我不确定如何去做。

我不明白是什么然后(如果合适),它调用num_get::get(使用流所选择的语言环境)来执行提取和解析操作,并相应地调整流的内部状态标志。最后,它在返回前销毁哨兵对象。

std::fstream filestr("ABC.txt", std::fstream::in);
if(!filestr.good()) //Logical error on i/o operation
{
  //Unable to process
  filestr.close();
  return;
}
unsigned index= 0;
unsigned count= 0;

while(filestr.good())
{
  float buffer= 0.f;
  filestr >> std::skipws >> buffer;
  score[index]= buffer;
  ++index;
}
filestr.close();

有很多方法可以做到这一点。一种方法是将stringstreams与vector和string结合使用:

#include <fstream>
#include <iostream>
#include <string>
#include <cstdlib>
#include <sstream>
#include <vector>
using namespace std;
int main() {
    std::ifstream filestr("C:\nums.txt", std::fstream::in);
    std::vector<double> numbers;
    if (!(filestr.good())) {
        std::cout << "BAD FILE" << std::endl;
        exit(0);
    }
    else {
        std::string temp;
        double d = 0.0;
        while(std::getline(filestr, temp)) {
        std::istringstream iss(temp);
        while(std::getline(iss, temp, ' ')) {
            std::istringstream ist(temp);
            ist >> f;
            numbers.push_back(f);
        }
    }
    }
    //see that the vector has information in it
    for (int i = 0; i < numbers.size(); i++) {
        std::cout << numbers[i] << std::endl;
    }
    filestr.close();
    return 0;
}

需要注意的一点是,这里也可以使用迭代器,但这是你可以自己实现的。

使用istream_iterator非常简单。下面的代码中只有一个棘手的地方。vector构造函数调用需要在第一个参数周围附加一组父元素,以避免Most Vexing Parse。

#include <fstream>                                                                                                        
#include <iostream>                                                                                                       
#include <iterator>                                                                                                       
#include <vector>                                                                                                         
#include <algorithm>                                                                                                      
using namespace std;                                                                                                      
int                                                                                                                       
main (int argc, char** argv)                                                                                              
{                                                                                                                         
    for (int i = 1; i < argc; ++i) {                                                                                      
        ifstream in (argv[i]);                                                                                            
        if (!in) {                                                                                                        
            cerr << "Failed to open " << argv[i] << endl;                                                                 
            continue;                                                                                                     
        }                                                                                                                 
        vector<double> nums ((istream_iterator<double> (in)), istream_iterator<double> ());                               
        copy (nums.begin (), nums.end (), ostream_iterator<double> (cout, "n"));                                         
    }                                                                                                                     
    return 0;                                                                                                             
}