Reading large values from text

hello i had a problem reading file.

in the text there are a huge number. This numbur is

37107287533902102798797998220837590246510135740250

how can i read this from the text and how can I split this number digit by digit into nodes? this nodes should save int.

int main() {

ifstream read_file;
read_file.open("numbers.txt");

if(!read_file.is_open()) {
cerr << "File could not be opened" << endl;
exit(1);
}

string line;
string numbers[100]; // There are 100 more numbers like this number.

int counter = 0;

while (getline(read_file, line)) {
numbers[counter] = line;
counter++;
}
read_file.close();

return 0;
}

my reading code is this but I could not continue the code.

help me please




Last edited on
Something like this:

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
#include <iostream>
#include <string>
#include <vector>
#include <cctype>
#include <fstream>

// get each digit in the line of text into a vector
// (ignore non-digit characters if any are present)
std::vector<int> get_digits( const std::string& line )
{
    std::vector<int> result ;

    for( char c : line ) // for each char in the line
    {
        // if it is a decimal digit, 
        // "the behavior of std::isdigit is undefined if the argument's value is 
        // neither representable as unsigned char nor equal to EOF
        // To use std::isdigit safely with plain chars, 
        // the argument should first be converted to unsigned char"
        // see: https://en.cppreference.com/w/cpp/string/byte/isdigit
        if( std::isdigit( static_cast<unsigned char>(c) ) )
            result.push_back( c - '0' ) ; // add the integer value to the vector
                                          // note that '7' - '0' == 7 etc.
        
        //  else there is an error: it is a badly formed line
    }

    return result ;
}

int main()
{
    const std::string input_file_name = "numbers.txt" ;

    if( std::ifstream file { input_file_name } ) // if the file was opened for input
    {
        std::string line ;
        while( std::getline( file, line ) ) // for each line in the file
        {
            std::cout << "line: " << line << '\n' ; // print out the line

            const auto digits = get_digits(line) ; // get the digits in the line
            
            std::cout << "there are " << digits.size() << " digits in the line.\nthey are: " ; 
            // print out the digits one by one
            for( int d : digits ) std::cout << d << ' ' ;
            std::cout << '\n' ;

            // TO DO: do something interesting with those digits
        }
    }

    else
        std::cerr << "error: failed to open input file '" << input_file_name << "'\n" ;
}
You can code it in the following way ( requires C++14 ):

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <iostream>
#include <fstream>
#include <vector>
#include <algorithm>
#include <iterator>

int main()
{
    std::ifstream file {"numbers.txt"};
    std::vector<int> data { std::istream_iterator<char>{file} , {} };
    std::transform( begin(data), end(data) , begin(data) , []( auto e ){ return e-'0';} );
    for( const auto& sign : data ) std::cout << sign << " ";
}


https://wandbox.org/permlink/SJSiLZ0xjGN8ZFKo

Note that all values in data that are outside the closed interval <0,9> are not valid digits.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;

int main()
{
   vector<int> digits;
   ifstream in( "numbers.txt" );
   for ( char c; in >> c; ) digits.push_back( c - '0' );
   for ( int i : digits ) cout << i << " ";
}



how can I split this number digit by digit into nodes?

What's a "node"?


There are 100 more numbers like this number.

More detail of what you are trying to do, please.

Last edited on
Seems to be this one.
https://projecteuler.net/problem=13

Big-number libraries at the ready then.
@gktrktrn7635
A simple extension of your program and using @lastchance conversion from char to int and std::list instead of writing your own node list you get the following:

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
#include <iostream>
#include <fstream>
#include <list> // or write your own or <vector>s or arrays 

using namespace std;

int main()
{
    std::list<int> list_of_ints;
    
    ifstream read_file;
    read_file.open("numbers.txt");
    
    if(!read_file.is_open())
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    }
    
    string line;
    string numbers[100]; // There are 100 more numbers like this number.
    
    int counter = 0;
    
    while (getline(read_file, line))
    {
        numbers[counter] = line;
        std::cout << numbers[counter] << '\n';
        
        for(int i = 0; i < line.length(); i++)
        list_of_ints.push_back(numbers[counter][i] - '0'); 
// cf @lastchance simple no-frills conversion
        counter++;
    }
    read_file.close();
    
    for(auto i: list_of_ints)
    {
        cout  << i << ' ';
    }
    cout << '\n';

    return 0;
}



37107287533902102798797998220837590246510135740250

3 7 1 0 7 2 8 7 5 3 3 9 0 2 1 0 2 7 9 8 7 9 7 9 9 8 2 2 0 8 3 7 5 9 0 2 4 6 5 1 0
 1 3 5 7 4 0 2 5 0 
Program ended with exit code: 0


Good luck using a std:list or your own NodeList in solving PE no. 13 if that's what it is.
Last edited on
FWIW I get (for the first 10 digits):
5537376230
Last edited on
PE etiquette dictates 'could be' :)
The OP doesn't actually need to split up the input - as long as he/she defines the appropriate addition operation he/she could just "sum" 100 strings.
Topic archived. No new replies allowed.