read digits from file and store them as separate integers

Hi, i have been trying to figure out how do i read some digits from a file and store them as separate numbers in an array, so far i can read it as one integer and use it but how do i divide a line of digits for example i have '1234' and i want them to be number[0]=1, number[1]=2 ...

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	

#include <iostream>
...

using namespace std;

int main ()
{
    // make file and write some digits in it
    ofstream makefile;
    makefile.open ("test.txt");
    makefile << "12";
    makefile.close();
    
    // ask to input filename and open it
    char filename [60];
    ifstream OpenFile;
    cin.getline (filename, 60);
    OpenFile.open (filename);
    
    // if this file does not exist then end the program
    if (!OpenFile.is_open())
    {
        exit(EXIT_FAILURE);
    }
    
    // read those digits as integers
    string str;
    int number[10];
    
    OpenFile >> str;
    stringstream MyStream(str);
    MyStream >> number[0];
    
    // check whether it is integer
    cout << "the integer is : " << number[0] << endl;
    cout << "integer + 3 equals to " << (number[0]+3) << endl;

    system ("pause");
    return 0;
}
My first topic here was actually about splitting an integer into a vector of its digits. :p
http://www.cplusplus.com/forum/beginner/44396/

The best thing to do then is this:

1
2
3
4
5
6
7
vector<char> intotvec(int tovec){
    stringstream ss;
    ss << tovec;
    string tmp = ss.str();
    vector<char> tmv(tmp.begin(),tmp.end());
    return tmv;
}


However, in your case I think it may be simpler to read your file as characters, instead of integers. Just read the first char, turn it into an integer and there is your digit.
Thanks, that's what i was looking for :) reading every element in char and then putting them into an int array ...

1
2
3
4
5
6
7
8
9
10
11
 // read those digits as integers
    char digit;
    int number[1000];
    int i = 0;

    while (OpenFile.good())
    {
        OpenFile >> digit;
        number[i]= digit - '0';
        i++;
    }
if i want to build it as an module so i can use this in other programs what should be the return value??

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
26
27
28
29
30
31
32
33
34
35
36
int ReadingDigits( char filename[])
{

    ifstream OpenFile;
    OpenFile.open (filename);

    // if this file does not exist then end the program
    if (!OpenFile.is_open())
    {
        exit(EXIT_FAILURE);
    }


    // read those digits as integers
    char digit;
    int number[1000];
    int i = 0;
    int nCount;

    while (OpenFile.good())
    {
        OpenFile >> digit;
        number[i]= digit - '0';
        i++;
        nCount = i-1;
    }


    OpenFile.close();

    cout << "there are " << nCount << " digits in this file" << endl;
    // check whether it is integer


   return ?? ; 
}
I assume you want to use the array of digits for something, so I'd say return the array of digits:

return number;

I'm not 100% sure of the syntax, I rarely use arrays. You may want to consider using a vector, because it's safer to use.
yes. thats correct, i do want to use that array for something :) but when i try to return the number there is an error: invalid conversion from 'int*' to 'int' as well as warning: address of local variable 'number' returned
Arrays are non returnable types.

When you return an array you're actually returning a pointer to its first element.

Using vectors is simplier and risk free. If however you insist in using arrays you need to change the return type to int*, you'll also need to know how many elements are in the array so that you can process it without going out of the array bondaries.

Have tou consider what happens if your file has more than 1000 digits?(It's not pretty =P)
If you're returning an array, you need to allocate the memory using new, and then remember to free it later. Which is why it's far better -- as eidge said - to use a vector.

The alternative is too horrible to say! (declare the array as static, so it doesn't disappear when you go out of scope). Returning a local variable will just blow up, as you no doubt know.

The old-school C-style approach to the array return problem is:

int ReadingDigits( const char* filename, char* buffer, int bufferSize )
{
...
}

In this case the return can be an error code (e.g. buffer to small)

P.S. I almost always see file name variable declared as (const) char*... I'm curious where the char[] habit comes from?
Last edited on
eidge , in this case i know that there won't be more than 1000 digits in the file. and i haven't faced the vectors yet so i am trying to do it this way. I am really a beginner but ready to learn :)

i am not sure whether i am using the pointers correctly here... when the program should execute this line:
cout << *Point << endl; it crashes.

if somebody could tell me why and don't go too harsh on me :D

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
using namespace std;

// ReadingDigits - reads digits from a txt file and puts them into an array of integers
int ReadingDigits(char* filename, int* Address);

int main()
{
    int* Point;
    char filename[60];
    cout << "enter the name of the file : ";
    cin.getline(filename, 60);

    ReadingDigits(filename, Point);


    cout << *Point << endl;

    system("pause");
    return 0;
}


int ReadingDigits( char* filename, int* Address)
{

    ifstream OpenFile;
    OpenFile.open (filename);

    // if this file does not exist then end the program
    if (!OpenFile.is_open())
    {
        cout << "can't open this file!" << endl;
        exit(EXIT_FAILURE);
    }


    // read those digits as integers
    char digit;
    int number[1000];
    int i = 0;
    int nCount;

    while (OpenFile.good())
    {
        OpenFile >> digit;
        number[i]= digit - '0';
        i++;
        nCount = i-1;
    }
    OpenFile.close();
    
    cout << "there are " << nCount << " digits in this file" << endl;
    Address = &number[0];
    
    // check whether it's working
    cout << (*Address+1) << endl;
}
Now i made an version where i don't need to use pointers or whatever but it still crashes... :(

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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using namespace std;

int main()
{
    char filename[60];
    cout << "enter the name of the file : ";
    cin.getline(filename, 60);

    ifstream OpenFile;
    OpenFile.open (filename);

    // if this file does not exist then end the program
    if (!OpenFile.is_open())
    {
        cout << "can't open this file!" << endl;
        exit(EXIT_FAILURE);
    }

    // read those digits as integers
    char digit;
    int number[1000];
    int i = 0;
    int nCount;

    while (OpenFile.good())
    {
        OpenFile >> digit;
        number[i]= digit - '0';
        i++;
        nCount = i-1;
    }
    OpenFile.close();

    cout << "there are " << nCount << " digits in this file" << endl;

    int GreatestProd = 0;
    int tempProduct;

    for (int t = 0; t < nCount; t++)
    {
        tempProduct = number[t];

        // PROGRAM WORKS TILL THIS PLACE

        for (int n = t; n <= (n+4); n++)
        {
            tempProduct *= number[n];
        }

        if (tempProduct > GreatestProd)
        {
            GreatestProd = tempProduct;
        }
    }

    cout << "the greatest product of five consecutive" << endl;
    cout << "digits in the given 1000-digit number is : " << GreatestProd << endl << endl;


    system("pause");
    return 0;
}
Why is the termination condition n+4 ???

As your stop value is always higher than the loop variable, this loop continues on past the end of your buffer and will eventually cause an exception.

1
2
3
4
        for (int n = t; n <= (n+4); n++)
        {
            tempProduct *= number[n];
        }


Andy

P.S. n + 4 is recalculated every loop. Maybe you mean

1
2
3
4
5
6
        const int n_max = n + 4;

        for (int n = t; n <= n_max; n++)
        {
            tempProduct *= number[n];
        }

Last edited on
ou yess :) somehow didn't really consider that :)

1
2
3
4
5
6
 const int n_max = n + 4;

        for (int n = t; n <= n_max; n++)
        {
            tempProduct *= number[n];
        }


This works as it should. Thanks.
Topic archived. No new replies allowed.