It's not correct to read the data from the file.



  • There's a code that reads the data from the text file and turns them on the screen and the other file.

    Here's the code:

    db_file.open(File_Zap.c_str(), ios::in); //объявлен чуть ранее экземпляр класса fstream
                        if (!db_file.is_open())  {
                                    cerr << "\n Open ERROR!";
                                    break;
                            } else {
                            // Вывод на экран 
                                cout << "\n\t--------- Informations ---------" << endl;
                            // Запись такой же информации в текстовый файл fout
                                fout << "\n\t--------- Informations ---------" << endl;
    
                            db_file.seekg(0, ios::end);       // выясняем размер файла
                            len = db_file.tellg();            // и записываем его в переменную
                            kol = len/size;                   // чтобы узнать количество записанных данных.
                            selection = new TZap[kol];
                            db_file.seekg(0, ios::beg);
                             while(!db_file.eof())
                                {
                                   readData(db_file, selection[counter]);
                                   Out(fout, selection[counter]);
                                   counter++;
                                }
                            db_file.close();      
                            delete []selection;
    

    //...
    void Out(ofstream & fout, TZap z)
    {
    cout << "\n ФИО – " << z.FIO
    << "\n Дата рождения – " << z.dateOfBirth
    << "\n Группа – " << z.group
    << "\n Оценка по физике – " << z.notes.fiz
    << "\n Оценка по математике – " << z.notes.math
    << "\n Оценка по информатике – " << z.notes.cs
    << "\n Оценка по химии – " << z.notes.chem
    << "\n Средний балл – " << z.s_ball
    << endl;

     fout &lt;&lt; "\n ФИО – " &lt;&lt; z.FIO 
     &lt;&lt; "\n Дата рождения – " &lt;&lt; z.dateOfBirth
     &lt;&lt; "\n Группа – " &lt;&lt; z.group
     &lt;&lt; "\n Оценка по физике – " &lt;&lt; z.notes.fiz
     &lt;&lt; "\n Оценка по математике – " &lt;&lt; z.notes.math
     &lt;&lt; "\n Оценка по информатике – " &lt;&lt; z.notes.cs
     &lt;&lt; "\n Оценка по химии – " &lt;&lt; z.notes.chem
     &lt;&lt; "\n Средний балл – " &lt;&lt; z.s_ball
     &lt;&lt; endl;
    

    }

    void calcGreatPointAverage(TZap* obj) {
    obj->s_ball = (obj->notes.chem + obj->notes.cs + obj->notes.math + obj->notes.fiz) / 4;
    }

    void readData(fstream & fs, TZap & object) {
    fs.getline(Zap.FIO, FIO_SIZE);
    fs.getline(Zap.dateOfBirth, BDAY_SIZE);
    fs.getline(Zap.group, GROUP_SIZE);
    fs >> object.notes.fiz
    >> object.notes.math
    >> object.notes.cs
    >> object.notes.chem

      &gt;&gt; object.s_ball;
    

    }

    File structure db_file This is how it works:

    Иванов И.И.
    12.12.1990
    123230
    1 2 3 4 2

    There's is some kind of garbage in the software launch: a bunch of systemic PATH, alphabet, big numbers and stuff. What's the problem?



  • Problem here:

    db_file.seekg(0, ios::end);
    

    The reader will then end the file and db_file.eof() Come back. true♪ Go back to the sign before reading, using

    db_file.seekg(0, ios::beg);
    

    In your case, reading will never be done and there will be debris in the variable.




Suggested Topics

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