C++ 標準檔案流以 StringToken 為單位讀取檔案並全部重新整理重寫

來源:互聯網
上載者:User

採用數組儲存中間變數,修改後一次性重新寫入

 

#include "stdafx.h"
 #include <cstdlib>
// Write a program that makes a virtual GET system.

// Write a program that saves and alters Student records saved to and from a file.
// A student should consist of a first name, last name,age, GPA (double), and units completed (int).
// Write a program that evaluates student information based on teacher input (of grades and units completed).
// Students must be defined in a class.

// Teacher input: What should be input is a numerical grade as well as units entered for a class.
// Professors must also be part of a class. Each professor, contains: a first name, last name, age, password.

// Note: Both Student and Professor Records Must be saved to files.

# include <cmath>  // header file for math functions
# include <iostream>   //  header file for all basics ie class, cin, cout, void
using namespace std;    //  standard namespace helps compiler translate program
#include <fstream>
#include <string>
#include <iomanip>

class student// class declaration
 {

 public : //  Encapsulating via data hiding; makes sure no external function can touch this data
    int units;   // any variable in this case scores
    double gpa;
     
    string firstname;
    string lastname;

    int age;
 
 };

class Professor
 {
 private:

  string firstname;
  string lastname;
 
  int age;
  string password;
 
 
 public:

  Professor( string fn, string ln, int a, string pw )
  {
      this->firstname = fn;
      this->lastname = ln;
      this->age = a;
      this->password = pw;
  }

  string getpassword()
  {
     return this->password;
  }
 
 
 };

int main()
{
    Professor * pr = new Professor("Lee", "Joel" , 30, "password");
   
    string pw = "";
    //while ( pw.compare(pr->getpassword())!=0 )
    {
    //   cout << "This is professor " << "Joel Lee, 30 years old, " << "Please enter your password: ";
    //   cin >> pw;
    }

    cout << "This is professor " << "Joel Lee, 30 years old,no password "  << endl;

    ifstream Indata;  //for the file

    Indata.clear(); 

    string filename;
    filename = "studentrecord.txt";

    int i = 0;
    int line = 0;
    string temp_word;
    Indata.open (filename.c_str());
    // 1. get the line of the file
    while(Indata.good())
    {
       if ( i != 0 && i % 4 == 0)
       {
           line ++;
         
       }
       Indata >> temp_word;
       i++;
    }

    Indata.close();

    //2. make the temporary memort to store

    student * stu = new student[line];

    ifstream Indata2;  //for the file
    Indata2.clear(); 

    Indata2.open (filename.c_str());
   
    i = 0;
    int nowline = 0;

    // 3. output and change the scores
    while(Indata2.good() && nowline < line)
    {
       int u;
       double g;

       if (i == 0)
       {

           Indata2 >> stu[nowline].firstname;
           cout << stu[nowline].firstname;

       }
       else if (i == 1)
       {

           Indata2 >> stu[nowline].lastname;
           cout << stu[nowline].lastname;

       }
       else if (i == 2)
       {

            Indata2 >> stu[nowline].units;
            cout << stu[nowline].units;
       }

       else if (i == 3)
       {

            Indata2 >> stu[nowline].gpa;
            cout << stu[nowline].gpa;
       }
       i++;
       if (  i  == 4 )
       {
           cout << endl;
           cout << "input the new unit: ";
           cin >> u;
           stu[nowline].units = u;
           cout << "input the new gpa: ";
           cin >> g;
           stu[nowline].gpa = g;
           nowline++;
           i = 0;

       }
  
       cout << " " ;
   
    }
    Indata2.close();

    // 4. write the new scores to file
   
   ofstream f1;
  // string fileout = "tabular.txt";
   f1.open(filename.c_str());
       if(!f1) return 1;
   for (int i = 0; i < line; i++)
   {
       f1<< stu[i].firstname << " ";
       f1<< stu[i].lastname << " ";
       f1<< stu[i].units << " ";
       f1<< setprecision(2) << stu[i].gpa << " " << endl;
   }

   f1.close();

    // end
    cout << endl;

    system("pause");

    return 0;
}

檔案格式:

 

paul lee 1 1
jack wa 1 1
joel joe 1 1

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.