Reading a stringstream 10 numbers at a time

If I have a very long sequence of numbers, how might I read them 10 at a time?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <sstream>
#include <string>

int main(){
	std::stringstream stream;
	// stream << "(thousands of numbers)";
	unsigned long long temp = 0;
	short int smallInt = 0;
	std::string tempString;
	stream.seekg(40);
	smallInt.get(temp, 10);
	
	std::cout << "temp = " << temp << std::endl;
	std::cout << std::endl;
}


This doesn't actually work.
Last edited on
OF course it doesn't work.
1
2
short int smallInt = 0;
smallInt.get(temp, 10);

A short int doesn't have a get method.

If the file is in text format you can use a loop to read 10 numbers.
In binary format you can read 10 numbers as a block
If the file is in text format

To keep the program running quickly, I think I would prefer to use sstream over fstream. I would just insert the sequence into the stream as shows above with
stream << "43364936946349"; and so on.

In binary format you can read 10 numbers as a block.

I don't understand what that means.
stream << "43364936946349";
What does it mean ? One long number or is each digit a separate number?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>

int main()
{
    std::istringstream stm( "01234567892535365789007676352418791476830807574352224586869912345" ) ;

    // read 10 characters (digits) at a time
    std::string s10 ;
    while( stm >> std::setw(10) >> s10 )
    {
        const long long int_val = std::stoll(s10) ; // may throw if the string is not well-formed
        std::cout << "string: " << std::quoted(s10) << "   integer value: " << int_val << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/ec026406826c1f18
using std::istreambuf_iterator<>:
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
# include <iostream>
# include <string>
# include <sstream>
# include <iomanip>
# include <iterator>
# include <vector>

int main()
{
    std::istringstream stm( "01234567892535365789007676352418791476830807574352224586869912345" ) ;

    std::istreambuf_iterator<char> eos;
    std::istreambuf_iterator<char> iit(stm);
    std::vector<std::string> numbersTenDigits{};
    std::string stringOfTen;

    while (iit != eos)
    {
        for (size_t i = 0; i <= 9; ++i)
        {
            stringOfTen += *iit++;
            if(i == 9)
            {
                numbersTenDigits.push_back(std::move(stringOfTen));
                stringOfTen.clear();
            }
        }
   }
   if(stringOfTen.size())numbersTenDigits.push_back(std::move(stringOfTen));
   for (const auto& elem : numbersTenDigits)std::cout << elem << "\n";
}
A less clumsy way to read n characters at a time directly from a stream buffer:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
#include <sstream>
#include <vector>

int main()
{
    std::istringstream stm( "253536578987907676352411476808075743522245868699961234" ) ;
    std::vector<std::string> vec ;

    char arr[10] ;
    std::streamsize n ;
    while( ( n = stm.rdbuf()->sgetn( arr, sizeof(arr) ) ) > 0 ) vec.emplace_back( arr, arr+n ) ;

    for( const std::string& str : vec ) std::cout << str << '\n' ;
}

http://coliru.stacked-crooked.com/a/517f8643cf661b29
Thanks. :)
Topic archived. No new replies allowed.