The task was: Create a custom iterator to work with files.
I also was told to use a proxy pattern which is new to me. Is there everything I can change to make my code look better?
Or maybe I have some slight errors which my compiler fixed
#include <iostream>
#include <cstdio>
#include <vector>
using namespace std;
class IntFile
{
public:
int value;
FILE* file;
IntFile()
{
file = fopen("text.txt", "r+b");
}
~IntFile()
{
fclose(file);
}
virtual int& getValue(int index)
{
fseek(file, 4*index, SEEK_SET);
fread(&value, 4, 1, file);
return value;
}
virtual int setValue(int value, int index)
{
fseek(file, 4*index, SEEK_SET);
fwrite(&value, 4, sizeof(value),file);
return 0;
}
class Proxy
{
private:
IntFile *ptr;
long index;
public:
Proxy(IntFile* ptr3, int idx)
{
ptr = ptr3;
index = idx;
}
Proxy& operator=(int value)
{
ptr->setValue(value, index);
}
operator int() const
{
return ptr->getValue(index);
cout << "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA" << endl;
}
};
friend struct iterator;
struct iterator
{
int index;
int value2;
IntFile* ptr;
iterator(IntFile* ptr2, int idx, FILE* ptrfile)
{
ptr = ptr2;
index = idx;
fseek(ptrfile, 4*index, SEEK_SET);
}
iterator (int i = 0) : value2(i)
{
}
bool operator==(const iterator&other) const
{
return index == other.index;
}
bool operator!=(const iterator&other) const
{
return index!=other.index;
}
Proxy operator*()
{
return Proxy(ptr, index);
}
int &operator=(int value)
{
this->value2 = value;
}
iterator &operator=(const iterator& other)
{
this->value2 = other.value2;
return *this;
}
iterator&operator++()
{
this->index = index+1;
}
iterator&operator--()
{
this->index = index -1;
}
iterator operator+=(int index2)
{
this->index = index + index2;
}
iterator operator+(const iterator& other)
{
return iterator(value2 + other.value2);
}
iterator operator-(const iterator& other)
{
return iterator(value2 - other.value2);
}
};
iterator begin()
{
return iterator(this, 0, file);
}
iterator end(int number)
{
return iterator(this, number, file);
}
iterator rbegin(int number)
{
return iterator(this, number-1, file);
}
iterator rend()
{
return iterator(this, -1, file);
}
};
int main()
{
IntFile myfile;
int number;
cout << "Enter number of elements: " << endl;
cin >> number;
vector <int> myVector;
cout << "Enter your numbers: ";
for ( int i = 0; i < number; i++)
{
cin >> myfile.value;
myVector.push_back(myfile.value);
}
fwrite(&myVector[0], sizeof(vector<int>::value_type), myVector.size(),myfile.file);
cout << endl << "FORWARD 1 by 1: " << endl;
for (IntFile::iterator i = myfile.begin(); i != myfile.end(number); ++i)
{
cout << *i << " ";
}
cout << endl << "BACKWARD 1 by 1: " << endl;
for (IntFile::iterator i = myfile.rbegin(number); i != myfile.rend(); --i)
{
cout << *i << " ";
}
IntFile::iterator i = myfile.begin();
/** *i = value **/
int a = 75;
*i = a;
cout << endl << endl << "75 should be here: " << *i << endl;
/** int value = *i **/
int k = 0;
k = *i;
cout << "76 should be here: " << k+1 << endl;
cout << endl;
return 0;
}
Here you can see my input and output just in case.
Input:
10, 1 2 3 4 5 6 7 8 9 10
Output:
FORWARD 1 by 1: 1 2 3 4 5 6 7 8 9 10
BACKWARD 1 by 1: 10 9 8 7 6 5 4 3 2 1
75 should be here: 75
76 should be here: 76
std::istream_iterator
\$\endgroup\$