How to read data into an array?
I have integers in a file, like: 324524 32452 213432
How do I read it into an array
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
|
int number;
input.open(inputfile.c_str() );
for(int x=0; x < 65000; x++)
{
input >> integerArray[x];
input >> integerArray[x];
}
output.open(outputfile.c_str());
while ( input >> x)
{
output << x << " ";
if (y >= 10)
{
output << endl;
y = 0;
}
y++;
}
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#include <vector>
#include <fstream>
#include <iostream>
int main()
{
std::vector<int> arr;
{
std::ifstream in ("myfile.txt");
unsigned x;
while(in >> x)
{
arr.push_back(x);
}
} //end of scope closes "in"
for(unsigned i = 0; i < arr.size(); ++i)
{
std::cout << arr[i] << std::endl;
}
}
|
324524
32452
213432 |
Last edited on
Topic archived. No new replies allowed.