读取输入文件并将它们存储在 C++ 中的 STL 向量中

问题描述

我正在尝试从文件中读取输入并尝试存储在名为 x_channel 的向量中。我正在尝试使用复制功能,但它没有将文件内容复制到 x_channel 向量中。我用来填充向量的代码copy(in_iter,eof,back_inserter(x_channel))。

我试图模仿 read integers from a file into a vector in C++ 中的这段代码

非常感谢

#include <iostream>
#include <fstream>
#include <vector>
#include <iterator>
#include <algorithm> // for copy

using namespace std;

int main() {
    ifstream infile("channels.txt",ios::in);
    
    // Get input stream and end of stream iterators 
    istream_iterator<double> in_iter(infile);
    istream_iterator<double> eof;

    // Get output stream iterators 
    ostream_iterator<double> cout_it(cout," ");
    vector<double> x_channel;

   // We have both input and output iterators,Now we can treat them // as containers. Using copy function we transfer data from one     // container to another.     // copy elements from input to output using copy functio
    copy(in_iter,cout_it);


    // copy elements from input to vector using copy function
    copy(in_iter,back_inserter(x_channel));

    cout << "\n";

    copy(x_channel.begin(),x_channel.end(),ostream_iterator<double>(cout,"\n"));

    cout << " " << endl;
    cout << "\n";

    infile.close();
        return 0; }

解决方法

第一个 copy() 正在从 infile 读取值并将它们打印到 std::cout。这就是将文件的读取指针移向文件末尾。因此,第二个 copy() 没有更多数据可以从 infile 读取以推入 x_channel。您需要将 infile 返回到文件的开头以再次重新读取相同的值。

此外,std::istream_iterator 无论如何都是一个单遍迭代器,因此您不能重复使用它来多次读取相同的数据。在向后查找文件后,您需要使用新的 std::istream_iterator 实例。

试试这个:

int main() {
    ifstream infile("channels.txt");
    vector<double> x_channel;
    
    copy(
        istream_iterator<double>(infile),istream_iterator<double>(),ostream_iterator<double>(cout," ")
    );

    infile.seekg(0);

    copy(
        istream_iterator<double>(infile),back_inserter(x_channel)
    );

    cout << "\n";

    copy(
        x_channel.begin(),x_channel.end(),"\n")
    );

    cout << " " << endl;
    cout << "\n";

    infile.close();
    return 0;
}

或者,就像@MooingDuck 建议的那样,一开始不要多次从 infile 中读取,只需从它读取一次到 x_channel,然后根据需要使用 x_channel (你真的需要用不同的分隔符在 cout 上重复复制相同的数据吗?),例如:

int main() {
    ifstream infile("channels.txt");

    vector<double> x_channel;
    copy(
        istream_iterator<double>(infile),back_inserter(x_channel)
    );

    /* alternatively:
    vector<double> x_channel(
        istream_iterator<double>(infile),istream_iterator<double>()
    );
    */

    infile.close();

    // omit this if you don't really need it...
    copy(x_channel.begin()," "));

    cout << "\n";

    copy(x_channel.begin(),"\n"));

    cout << " " << endl;
    cout << "\n";

    return 0;
}
,

您不需要将文件“复制”到向量中。 “复制”是指创建某物的副本。 您要做的只是“读取文件并将数据存储在矢量容器中”。

这很简单,就像这个最小的可执行示例所示:

#include <iostream>
#include <fstream>
#include <vector>

#define FILENAME "channels.txt"

int main()
{
  std::fstream infile;
  std::vector<double> xChannel;

  xChannel.push_back(123);
  xChannel.push_back(456);

  //Print vector
  for(double element : xChannel)
    std::cout << element << std::endl;

  //Write data to file for testing purpose
  infile.open(FILENAME,std::ios::out);
  if(!infile.is_open())
    std::cout << "Could not open file!" << std::endl;

  //write elements to file with for:each loop
  for(double element : xChannel)
    infile << element << std::endl;

  //Close file
  infile.close();
  //Delete vector items for proof of conecpt
  xChannel.clear();

  //Read data back into vector from file
  infile.open(FILENAME,std::ios::in);
    if(!infile.is_open())
      std::cout << "Couldnot open file!" << std::endl;

  //Read data back to vector
  double value{};
  while(infile >> value){
    xChannel.push_back(value);
  }
  //Close file
  infile.close();

  //Print vector
  for(double element : xChannel)
    std::cout << element << std::endl;

  std::cin.get();
  return 0;
}

一旦你在数据中变红,你就可以处理向量 随意,以及复制(复制)值。

您可以在 std::vectorhere 上找到可以使用的方法。 F.e.对向量进行排序只需使用代码行 std::sort(xChannel.begin(),xChannel.end()); 你会从中得到很多乐趣:)

顺便说一句。要阅读整行,您可以使用 std::getline() - 它会从文件中获取整行代码。