Filling a 1D Array with a ".txt" file

Sep 18, 2010 at 11:27pm
I want to fill two one dimensional arrays from two different .txt files. How would I write a code for that? I tried using the fstream library and the ifstream input command but that doesn't seem to work. Anyone?

The program when executed should automatically fill one array with one ".txt" file and the other one should be filled in as well from a different ".txt" file so they can be compared in a seperate function. So no manual inputs.
Sep 18, 2010 at 11:39pm
Use a std::istream_iterator adapter, std::back_inserter adapter and std::copy:

1
2
3
4
5
6
7
8
9
10
#include <fstream>
#include <iterator>
#include <algorithm>

using namespace std;

fstream input("input.txt");
vector<char> data;

copy(input_iterator<char>(input), input_iterator<char>(), back_inserter(data));


Sep 18, 2010 at 11:43pm
Is this the only way to achieve that?
I haven't' seen some of this stuff before so I don't really know what the vector<char> does. I haven't learned that much yet.
Sep 19, 2010 at 12:00am
std::vector is what C++ programmers typically use in place of simple arrays.

You can learn about vectors here: http://www.cplusplus.com/reference/stl/vector/
Topic archived. No new replies allowed.