Модераторы: bsa
  

Поиск:

Ответ в темуСоздание новой темы Создание опроса
> Итератор для работы с файлом. Не работает созданный итератор 
:(
    Опции темы
AlexMiel
Дата 28.9.2017, 22:55 (ссылка) | (нет голосов) Загрузка ... Загрузка ... Быстрая цитата Цитата


Новичок



Профиль
Группа: Участник
Сообщений: 4
Регистрация: 28.9.2017

Репутация: нет
Всего: нет



Всем привет. Возник вопрос. Задача: сделать свой итератор и считывать инты из бинарного файла. Мой итератор пока вообще не работает, не заходит в цикл, хотя вроде логика есть. Какие нибудь советы?

Код

#include <iostream>
#include <cstdio>


using namespace std;


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


    int getValue(int index)
    {
        fseek(file, 4*index, SEEK_SET);
        fread(&value, 4, 1, file);
        return value;
    }

    friend struct iterator;
    struct iterator
    {
        int index = 0;
        int value = 0;

        bool operator=(const iterator&other) const
        {
            return index == other.index;
        }
        bool operator!=(const iterator&other) const
        {
            return index!=other.index;
        }

        int &operator*()
        {
            //return value = IntFile::getValue(int index);
        }
        iterator&operator++()
        {
            this->index = index+1;
        }
        iterator&operator--()
        {
            index = index -1;
        }
    };
    iterator begin()
    {
        fseek(file, 0, SEEK_SET);
        //index = 0;
    }
    iterator end()
    {
        fseek(file, 0, SEEK_END);
        //index = 9;
    }
};



int main()
{
    IntFile file;
    int A;
    // Getting numbers from user and putting them to array
    /*
    cout << "Enter your numbers: ";
    for ( int i = 0; i < 10; i++)
    {
        cin >> file.value;
        file.mnumbers[i] = file.value;
        //cout << file.mnumbers[i] << " ";
    }
    cout << endl;


    file.file = fopen("text.txt", "r+b");
    fwrite(file.mnumbers,4,sizeof(file.mnumbers),file.file);
    fclose(file.file);
    */

    for ( int i = 0; i < 10; i++)
    {
        file.mnumbers2[i] = 0;
        cout << file.mnumbers2[i] << " ";
    }


    file.file = fopen("text.txt", "r+b");
    for (IntFile::iterator Iter = file.begin(); Iter != file.end(); ++Iter)
    {
        fread(file.mnumbers2,4,1,file.file);
        cout << " I " << endl;
        //cout << "Iterator: " << Iter;
    }

    cout << endl;
    A = file.getValue(9);
    cout << "A: " << A << endl;

    //cout << "array : ";
    for ( int i = 0; i < 10; i++)
    {
        cout << file.mnumbers2[i] << " ";
    }


    ////////////////////////////////////////
    fclose(file.file);
    return 0;
}

PM MAIL   Вверх
  
Ответ в темуСоздание новой темы Создание опроса
Правила форума "C/C++: Для новичков"
JackYF
bsa

Запрещается!

1. Публиковать ссылки на вскрытые компоненты

2. Обсуждать взлом компонентов и делиться вскрытыми компонентами

  • Действия модераторов можно обсудить здесь
  • С просьбами о написании курсовой, реферата и т.п. обращаться сюда
  • Вопросы по реализации алгоритмов рассматриваются здесь


Если Вам понравилась атмосфера форума, заходите к нам чаще! С уважением, JackYF, bsa.

 
0 Пользователей читают эту тему (0 Гостей и 0 Скрытых Пользователей)
0 Пользователей:
« Предыдущая тема | C/C++: Для новичков | Следующая тема »


 




[ Время генерации скрипта: 0.2536 ]   [ Использовано запросов: 22 ]   [ GZIP включён ]


Реклама на сайте     Информационное спонсорство

 
По вопросам размещения рекламы пишите на vladimir(sobaka)vingrad.ru
Отказ от ответственности     Powered by Invision Power Board(R) 1.3 © 2003  IPS, Inc.