opening number text file

how do i save a text file into an int array? the code i'm trying is as follows.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <fstream>

using namespace std;

int main()
{
    ofstream myfile;
    int a[1000] = {0};
    myfile.open ("Euler7.txt", ios::out | ios::app | ios::binary);
    for (int i=0;i<1000;i++)
    {
        myfile << a[i];
        cout << a[i] << endl;
    }
    myfile.close();
    int c = 0;
    for (int b=0;b>995;b++)
    {
        if (a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4] > c)
            c = a[b]*a[b+1]*a[b+2]*a[b+3]*a[b+4];
    }
    cout << c << a;
    return 0;
}

but everything i try to save just ends up being 0, the txt file is formatted like 651561561841351641351468746549, and i want to pull 1 digit intergers from it
Last edited on
ofstream That is the output stream. You are not "reading from", you are "saving to" this file.
myfile << a[i]; Again, this is an output operation.
If you want to read ints without separators, you should read chars one by one and convert each to int.

Is this a problem to find maximum product of five consequent digits?
yeah, euler 8, i'm going through and using c++ to solve all the ones i've solved with ruby. now that i'm reading from i'm working on the atoi.
In most cases (when you are using ASCII) you can just do
1
2
//c is char
int x = c - '0';

I get this problem in 23 lines.
Topic archived. No new replies allowed.