The C ++ standard file stream reads the file in stringtoken units and refresh and rewrite all

Source: Internet
Author: User

Use an array to save the intermediate variable. After modification, write the variable again.

 

# 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 shoshould 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 shocould be input is a numerical grade as well as units entered for a class.
// Professors must also be part of a class. Each pointer SOR, contains: a first name, last name, age, password.

// Note: Both Student and pointer sor 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 aggregsor
{
PRIVATE:

String firstname;
String lastname;

Int age;
String password;


Public:

Cursor sor (string FN, string ln, int A, string PW)
{
This-> firstname = FN;
This-> lastname = ln;
This-> age =;
This-> Password = pw;
}

String GetPassword ()
{
Return this-> password;
}


};

Int main ()
{
Partition sor * Pr = new partition sor ("Lee", "Joel", 30, "password ");

String PW = "";
// While (PW. Compare (pr-> GetPassword ())! = 0)
{
// Cout <"this is another sor" <"Joel Lee, 30 years old," <"Please enter your password :";
// CIN> PW;
}

Cout <"this is another sor" <"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;
}

File Format:

 

Paul Lee 1 1
Jack wa 1 1
Joel Joe 1 1

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.