Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read a file into vector in C++?

Tags:

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 
like image 722
Orgmo Avatar asked Feb 28 '13 15:02

Orgmo


People also ask

How do I read a vector file?

To open a vector image in Google Chrome, for instance, click File > Open File and select your vector image file. If it's in a standard format like SVG, Chrome should generate the image and allow you to view it. SVG images can also be opened in text editors like Notepad++, as SVG files are merely lines of XML code.

How do you vector output?

In the for loop, size of vector is calculated for the maximum number of iterations of loop and using at(), the elements are printed. for(int i=0; i < a. size(); i++) std::cout << a.at(i) << ' '; In the main() function, the elements of vector are passed to print them.

Can we use vector in C?

Vectors are a modern programming concept, which, unfortunately, aren't built into the standard C library. Vectors are same as dynamic arrays with the ability to resize itself automatically when an element is inserted or deleted, with their storage being handled automatically by the container.

What does vector data () do?

vector data() function in C++ STL The std::vector::data() is an STL in C++ which returns a direct pointer to the memory array used internally by the vector to store its owned elements.


1 Answers

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.

like image 124
juanchopanza Avatar answered Sep 22 '22 14:09

juanchopanza