Creating a struct Iterator to work with files.

Hello. I have a binary INT file with values there. So 1 int takes 4 bytes right? So as i can see, my iterator should increase a index so i can move to the next element, but I'm not sure about that. Also, I cant use "index" in my iterator, because it's not declared here, so how do i manage it? Hope my question is clear. Or maybe this whole idea is not correct

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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#include <iostream>
#include <cstdio>


using namespace std;


class IntFile
{
public:
    FILE* file;
    IntFile() {}
    ~IntFile() {}
    int mnumbers[10];
    int mnumbers2[10];
    int mnumbers3[10];
    int value;

    friend struct iterator;
    struct iterator
    {
        int index = 0;
        int checker = 0;
        bool operator++()
        {
            index++;
        }
        bool operator--()
        {
            index--;
        }
        bool operator==(const iterator&other) const
        {
            return index==other.index;
        }
        bool operator!=(const iterator&other) const
        {
            return index!=other.index;
        }
    };
    iterator begin()
    {
        index = 1;
    }
    iterator end()
    {
        index = 10;
    }
};



int main()
{
    IntFile MyFile;

    ////////////////////////////////////////
    cout << "Enter your numbers: ";
    for ( int i = 0; i < 10; i++)
    {
        cin >> MyFile.value;
        MyFile.mnumbers[i] = MyFile.value;
        cout << MyFile.mnumbers[i] << " ";
    }
    cout << endl;
    
    MyFile.file = fopen("text.txt", "r+b");
    fwrite(MyFile.mnumbers,4,10,MyFile.file);
    fclose(MyFile.file);
    ////////////////////////////////////////


    ////////////////////////////////////////
    for (IntFile::iterator Iter = MyFile.begin(); Iter != MyFile.end(); ++Iter)
    {
        MyFile.file = fopen("text.txt", "r+b");
        fread(MyFile.mnumbers2,4*(MyFile.index),1,MyFile.file);
        fclose(MyFile.file);
    }
    
    cout << "RESULT OF ++: " << endl;
    for ( int i = 0; i < 10; i++)
    {
        cout << MyFile.mnumbers2[i] << " ";
    }

    ////////////////////////////////////////
    return 0;
}


Last edited on
you started a thread on a similar topic few days back - http://www.cplusplus.com/forum/beginner/221747/
four people (incl me) responded. You gave us no feedback on the replies and now there's another very similar post - do the decent thing and tell us first where you got to with the previous one
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
#include <iostream>
#include <iterator>
#include <type_traits>
#include <fstream>
#include <algorithm>

template < typename T > struct as_bits
{
    static_assert( std::is_trivially_copyable<T>::value, "T must be a TriviallyCopyable type" ) ;
    static_assert( std::is_default_constructible<T>::value, "T must be a DefaultConstructible type" ) ;

    as_bits( const T& v = {} ) : value(v) {}
    operator T() const { return value ; }
    T value ;

    friend std::istream& operator>> ( std::istream& stm, as_bits<T>& bits )
    { return stm.read( reinterpret_cast<char*>( std::addressof(bits.value) ), sizeof(bits.value) ) ; }

    friend std::ostream& operator<< ( std::ostream& stm, const as_bits<T>& bits )
    { return stm.write( reinterpret_cast< const char* >( std::addressof(bits.value) ), sizeof(bits.value) ) ; }
};

template < typename T >
using binary_istream_iterator = std::istream_iterator< as_bits<T> > ;

template < typename T >
using binary_ostream_iterator = std::ostream_iterator< as_bits<T> > ;

int main()
{
    const int test[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 } ;
    const char* const file_name = "test.bin" ;

    {
        std::ofstream file( file_name, std::ios::binary ) ;
        std::copy( std::begin(test), std::end(test), binary_ostream_iterator<int>(file) ) ;
        file.clear() ;
        std::cout << "bytes written: " << file.tellp() << "  array size: " << sizeof(test) << '\n' ;
    }

    {
        std::ifstream file( file_name, std::ios::binary ) ;
        binary_istream_iterator<int> begin(file), end ;
        std::copy( begin, end, std::ostream_iterator<int>( std::cout, " " ) ) ;
        std::cout << '\n' ;
    }
}

http://rextester.com/MEUMG68174
@gunnerfunner my deep apologies for that
@JLBorges the main issue is that i need to create my own iterator.
> i need to create my own iterator.

If you have been asked to do that, you can't be a newbie in C++.

Unannotated code under the assumption that you must already know quite a bit about iterators:
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <iostream>
#include <iterator>
#include <type_traits>
#include <fstream>
#include <algorithm>

namespace binary_io
{
    template < typename T > // use needlessly deprecated std::iterator for brevity
    struct istream_iterator : std::iterator< std::input_iterator_tag, T, T >
    {
        static_assert( std::is_trivially_copyable<T>::value, "T must be a TriviallyCopyable type" ) ;
        static_assert( std::is_default_constructible<T>::value, "T must be a DefaultConstructible type" ) ;

        istream_iterator() : stm(nullptr) {}
        explicit istream_iterator( std::istream& stm ) : stm( std::addressof(stm) ) { ++*this ; }

        T operator* () const { return value ; }
        const T* operator->() const { return std::addressof(**this) ; }

        istream_iterator& operator++()
        {
            stm->read( reinterpret_cast<char*>( std::addressof(value) ), sizeof(value) ) ;
            return *this ;
        }
        istream_iterator& operator++(int) { return ++*this ; }

        bool operator== ( const istream_iterator& that ) const { return bool(*this) == bool(that) ; }
        bool operator!= ( const istream_iterator& that ) const { return !( *this == that) ; }

        std::istream* stm ;
        T value {} ;

        explicit operator bool() const { return stm && *stm ; }
    };

    template < typename T > // use needlessly deprecated std::iterator for brevity
    struct ostream_iterator : std::iterator< std::output_iterator_tag, T, T >
    {
        static_assert( std::is_trivially_copyable<T>::value, "T must be a TriviallyCopyable type" ) ;

        explicit ostream_iterator( std::ostream& stm ) : stm(stm) {}

        ostream_iterator& operator* () { return *this ; }

        ostream_iterator& operator++() { return *this ; }
        ostream_iterator& operator++(int) { return ++*this ; }

        ostream_iterator& operator= ( const T& value )
        {
            stm.write( reinterpret_cast< const char* >( std::addressof(value) ), sizeof(value) ) ;
            return *this ;
        }

        std::ostream& stm ;
    };
}

int main()
{
    const long long test[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 } ;
    const char* const file_name = "test.bin" ;

    {
        std::ofstream file( file_name, std::ios::binary ) ;
        std::copy( std::begin(test), std::end(test), binary_io::ostream_iterator<long long>(file) ) ;

        // sanity check
        std::cout << "bytes written: " << file.tellp() << "  array size: " << sizeof(test)
                  << "  #items: " << sizeof(test) / sizeof(*test) << '\n' ;
    }

    {
        std::ifstream file( file_name, std::ios::binary ) ;
        binary_io::istream_iterator<long long> begin(file), end ;
        std::copy( begin, end, std::ostream_iterator<long long>( std::cout, " " ) ) ;

        // sanity check
        file.clear() ;
        const auto pos = file.tellg() ;
        std::cout << "\nbytes read: " << pos << "  #items: " << pos / sizeof(long long) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/6723d2a192d9f66a
http://rextester.com/PANCQ22255
Topic archived. No new replies allowed.