Data from the file. Formation of the mass(s) from the line where the dividers are present



  • Respected Forumans. Please don't put your caps on, you've got both a forum and other sources of information, but you never found information that fits my understanding. I'm writing a course of work on the search for the master of the matrix using classes. There's one place where the shuttle happened, I don't know how to run the file. The logic of the thought: There's a file, it's got values, we'll suggest through the gap, these values and will be the meaning of the matrix elements. I mean, in the file: 11 12 13 14 M[0][0] = 11, M[0][1] = 12, M[1][0] = 13, M[1][1] = 14, but how do this occur? This bite gives the value of the lines in the file and, accordingly, the lines in the area for further dynamic remembrance. That's the same thing in the columns, I'm not gonna make that mind. Help, people are good. I'm more grateful.

            ifstream matrix;
            matrix.open(path, ios::app);
            ROWS=1;
            COLS=1;
            if (!matrix)
            {
                cout << "Файл не может быть открыт!\n";
            }
            else
            {
                matrix >> str;
                while (true)
                {
                    getline(matrix, str);
                    if (!matrix.eof())
                        ROWS++;
                    else
                        break;
                }
                return arr;
    
            matrix.close(); 
    

    Just in case I put the whole code on.

    #include <iostream>
    #include <time.h>
    #include <fstream>
    #include <string>
    #include <sstream>
    using namespace std;
    class Array
    {
    public:
    int get_params()
    {
    cout << "Введите количество столбцов матрицы=";
    cin >> ROWS;
    cout << "Введите количество строк матрицы=";
    cin >> COLS;
    return 0;
    }

    int** memory_allocate_array()
    {
        arr = new int* [ROWS];
        for (int i = 0; i &lt; ROWS; i++)
        {
            arr[i] = new int[COLS];
        }
        return arr;
    }
    int** put_array_random()
    {
        for (int row = 0; row &lt; ROWS; row++)
        {
            for (int col = 0; col &lt; COLS; col++)
            {
                arr[row][col] = rand() % 2;
            }
    
        }
        return arr;
    }
    int** put_array_user()
    {
        for (int row = 0; row &lt; ROWS; row++)
        {
            for (int col = 0; col &lt; COLS; col++)
            {
                cout &lt;&lt; "Введите arr[" &lt;&lt; row &lt;&lt; "][" &lt;&lt; col &lt;&lt; "]=";
                cin &gt;&gt; arr[row][col];
            }
    
        }
        return arr;
    }
    int output_array()
    {
        for (int row = 0; row &lt; ROWS; row++)
        {
            for (int col = 0; col &lt; COLS; col++)
            {
                cout &lt;&lt; "arr[" &lt;&lt; row &lt;&lt; "][" &lt;&lt; col &lt;&lt; "]=" &lt;&lt; arr[row][col] &lt;&lt; endl;
            }
        }
        return 0;
    }
    int** work_with_file()
    {
        ifstream matrix;
        matrix.open(path, ios::app);
    
        if (!matrix)
        {
            cout &lt;&lt; "Файл не может быть открыт!\n";
        }
        else
        {
            matrix &gt;&gt; str;
            
            while (true)
            {
                getline(matrix, str);
                if (!matrix.eof())
                    ROWS++;
                else
                    break;
            }
            cout &lt;&lt; "ROWS= " &lt;&lt; ROWS &lt;&lt; "COLS= " &lt;&lt; COLS;
            return arr;
    
            matrix.close(); // закрываем файл
        }
    
    }
    

    private:
    int** arr = NULL;
    int ROWS = 0;
    int COLS = 0;
    string str;
    string path = "matrix.txt";
    /*
    *
    * Подумать как реализовать деструктор, нужен доступ к arr, который является закрытым членом класса
    *
    *
    * ~Array()
    * {
    * for (int col = 0; col < COLS; col++)
    * {
    * delete[]arr[col];
    * }
    * delete[]arr;
    * cout << "Деструктор класса отработал" << endl;
    *
    * }
    */

    };
    int main()
    {
    setlocale(0, "Rus");
    srand(time(NULL));
    int var;
    Array array;
    cout << "Введите каким способом вы хотите заполнить матрицу" << endl << "1. Рандомно" << endl << "2. Самостоятельно" << endl << "3. Из файла" << endl;
    cin >> var;
    switch (var)
    {
    case 1:
    {
    cout << "Вы выбрали рандомное заполнение матрицы" << endl;
    array.get_params();
    array.memory_allocate_array();
    array.put_array_random();
    }
    break;
    case 2:
    {
    cout << "Вы выбрали самостоятельное заполнение матрицы" << endl;
    array.get_params();
    array.memory_allocate_array();
    array.put_array_user();
    }
    break;
    case 3:
    {
    cout << "Укажите путь к файлу" << endl;
    array.work_with_file();
    }
    break;
    default:
    {
    cout << "Ты че, пес, тупой что ли?";
    }
    }
    array.output_array();
    return 0;
    }



  • It's easier to get the numbers in the line around:

    int values_count(const string& str)
    {
        istringstream is(str);
        int val = 0;
        double x;
        while(is >> x) val++;
        return val;
    }
    

    Don't forget to compare the values for all the lines.

    I don't see the point yet.

    string str;
    string path = "matrix.txt";
    

    are members of the class. Why? ♪ ♪



Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2