如何在C ++中将文件读入向量? [英] How to read a file into vector in C++?

查看:236
本文介绍了如何在C ++中将文件读入向量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要读取 .data .txt 文件,其中包含新的将每行的浮点数编号到一个向量中。

我已经搜索了很多并且应用了许多不同的方法,但是每次得到相同的结果, Main.size() code> 0 和一个错误说矢量下标超出范围,所以显然这个矢量是只是没有读取任何文件。
$ b

注意:文件既在文件夹中也包含在VS项目中。



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

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

使用namespace std;

int main(){

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

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

双数;

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

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

cin.get();
返回0;

$ / code $ / pre
$ b $ p我得到的结果总是简单的:

 编号:
0


  for(int i = 0; i = 0)解决方案

试试这个:

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

另外,将数字读入向量并将其写入标准输出的更习惯方式是沿着这些线的:

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

int main()
{
std :: ifstream is(numbers.txt);
std :: istream_iterator< double>开始(是),结束;
std :: vector< double>数字(开始,结束);
std :: cout<< 阅读<< numbers.size()<< 数字<<的std :: ENDL;

//将数字打印到标准输出
std :: cout<< 数字读入:\ n;
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.

I 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.

Note: 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 ++中将文件读入向量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆