G
G
GreatSpacer112020-06-09 16:40:07
C++ / C#
GreatSpacer11, 2020-06-09 16:40:07

C++ How to read data from file into 3 different arrays?

Hello everyone, the task is this: "it is necessary to generate three random numeric arrays
with a size of 500 elements, display these arrays on the screen and in a text file, reset the
arrays in memory, display them again, read the previously written
arrays from the text file"
I described the class an array that contains the array itself and methods for generating, writing to a file, zeroing, and reading.
stuck in place reading from file

void readFile()
  {
    ifstream file_read; // создание объекта для чтения массива
    file_read.open("massivs.txt"); // открытие файла
    for (i = 0; i < SIZE; i++)
    {
      file_read >> razmer[i];
    }
  }

When reading each reading, the file is opened anew and only the first i values ​​are read. How to make it remember where it stopped, or how else to solve this problem?
void zapisFile()
  {
    ofstream file_zapis;				
    file_zapis.open("massivs.txt", ios::app);		// открытие файла (добавление в конец)
    if (!file_zapis) {					// проверка открылся ли файл
      cout << "file net";
    }
    else
    {
      for (i = 0; i < SIZE; i++)		// запись массива в файл
      {
        file_zapis << razmer[i] << " ";
      }

      file_zapis.close();

    }

massiv a;
  massiv b;
  massiv c;
  a.generatee();
  b.generatee();
  c.generatee();
  cout << "Massiv a:";
  a.vivod();
  cout << "Massiv b:";
  b.vivod();
  cout << "Massiv c:";
  c.vivod();

  a.zapisFile();
  b.zapisFile();
  c.zapisFile();

  a.clearMassiv();
  b.clearMassiv();
  c.clearMassiv();
  a.readFile();
  a.vivod();

  b.readFile();
  b.vivod();

  c.readFile();
  c.vivod();

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Shitskov, 2020-06-09
@GreatSpacer11

For example, describe a class that creates N instances of the array class, and implement write and read methods in this class, not in array classes. Then the counter will be stored in one place - in the object of this new class

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question