本文介绍了C ++读文件到矢量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从一个.data或.txt文件中读取每一行上的一个新的浮点数到一个向量,已经搜索了远,宽,并应用了许多不同的方法,但每次我获得相同的结果。 size()为0和一个错误说向量下标超出范围,所以显然向量只是没有读入任何东西到文件中。





无论如何,这里是我的代码:

 

code> #include< iostream>
#include< fstream>
#include< sstream>
#include< vector>
#include< string>

using namespace std;

int main(){

vector< double>主要;
int count;
string lineData;
double tmp;

ifstream myfile(test.data,ios :: in);

double number;

myfile>>计数;
for(int i = 0; i myfile>> tmp;
Main.push_back(tmp);
cout<<计数;
}

cout<< Numbers:\\\
;
cout<< Main.size();
for(int i = 0; i =((Main.size()) - 1); i ++){
cout< Main [i]<< '\\\
';
}

cin.get();
return 0;
}

我得到的结果总是简单的:

 数字:
0


  for(int i = 0; i = ((Main.size()) -  1); i ++){

>

  for(int i = 0; i< Main.size(); i ++){

此外,一个更常用的方法是将数字读入向量并将其写入stdout:

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

int main()
{
std :: ifstream is(numbers.txt);
std :: istream_iterator< double> start(is),end;
std :: vector< double>数字(开始,结束);
std :: cout<< 读取<< number.size()<< 数字<< std :: endl;

//将数字打印到stdout
std :: cout<< 数字读入:\\\
;
std :: copy(numbers.begin(),numbers.end(),
std :: ostream_iterator< double>(std :: cout,));
std :: cout<< std :: endl;

}

虽然您应该检查 ifstream 读取错误。


I need to read from a .data or .txt file containing a new float number on each line into a vector, have searched far and wide and applied numerous different methods but every time I get the same result, of a Main.size() of 0 and an error saying "Vector Subscript out of Range", so evidently the vector is just not reading anything into the file.

the file is both in the folder and also included in the VS project.

Anyway, here's my code:

#include <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

using namespace std;

int main() {

    vector<double> Main;
    int count;
    string lineData;
    double tmp;

    ifstream myfile ("test.data", ios::in);

    double number;

    myfile >> count;
    for(int i = 0; i < count; i++) {
        myfile >> tmp;
        Main.push_back(tmp);
        cout << count;
    }

    cout << "Numbers:\n";
    cout << Main.size();
    for (int i=0; i=((Main.size())-1); i++) {
        cout << Main[i] << '\n';
    }

    cin.get();
    return 0;
}

The result I get is always simply:

Numbers:
0
解决方案

Your loop is wrong:

for (int i=0; i=((Main.size())-1); i++) {

Try this:

for (int i=0; i < Main.size(); i++) {

Also, a more idiomatic way of reading numbers into a vector and writing them to stdout is something along these lines:

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

int main()
{
  std::ifstream is("numbers.txt");
  std::istream_iterator<double> start(is), end;
  std::vector<double> numbers(start, end);
  std::cout << "Read " << numbers.size() << " numbers" << std::endl;

  // print the numbers to stdout
  std::cout << "numbers read in:\n";
  std::copy(numbers.begin(), numbers.end(),
            std::ostream_iterator<double>(std::cout, " "));
  std::cout << std::endl;

}

although you should check the status of the ifstream for read errors.

这篇关于C ++读文件到矢量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 09:46
查看更多