0

示例 ABC.txt 10.f 30.2f 20.f

我想检索这些信息并存储在我的数组中。但是我不确定该怎么做。

我不明白 Then 是什么(如果好的话),它调用 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();
4

2 回答 2

0

使用 istream_iterator 超级简单。下面的代码中只有一个棘手的地方。向量构造函数调用需要围绕第一个参数的一组额外的括号,以避免最令人烦恼的 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;                                                                                                             
}                
于 2013-03-17T15:15:08.530 回答
0

有很多方法可以做到这一点。一种方法是使用字符串流,结合向量和字符串:

#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;
}

需要注意的一点是,您也可以在此处使用迭代器,但您可以自己实现。

于 2013-03-17T15:21:52.070 回答