如何在C++中从文件读取/写入文件的类对象?

2021年3月14日14:38:22 发表评论 567 次浏览

给定文件" Input.txt", 其中每一行的值均与类的实例变量相同。

将值读入类的对象并进行必要的操作。

理论:

The data transfer is usually done using '>>'
and <<' operators. But if you have
a class with 4 data members and want 
to write all 4 data members from its
object directly to a file or vice-versa, we can do that using following syntax :

To write object's data members in a file :
// Here file_obj is an object of ofstream
file_obj.write((char *) & class_obj, sizeof(class_obj));

To read file's data members into an object :
// Here file_obj is an object of ifstream
file_obj.read((char *) & class_obj, sizeof(class_obj));

例子:

Input : 
Input.txt :
Michael 19 1806
Kemp 24 2114
Terry 21 2400
Operation : Print the name of the highest 
            rated programmer.

Output : 
Terry

C ++

// C++ program to demonstrate read/write of class
// objects in C++.
#include <iostream>
#include <fstream>
using namespace std;
 
// Class to define the properties
class Contestant {
public :
     // Instance variables
     string Name;
     int Age, Ratings;
 
     // Function declaration of input() to input info
     int input();
 
     // Function declaration of output_highest_rated() to
     // extract info from file Data Base
     int output_highest_rated();
};
 
// Function definition of input() to input info
int Contestant::input()
{
     // Object to write in file
     ofstream file_obj;
 
     // Opening file in append mode
     file_obj.open( "Input.txt" , ios::app);
 
     // Object of class contestant to input data in file
     Contestant obj;
 
     // Feeding appropriate data in variables
     string str = "Michael" ;
     int age = 18, ratings = 2500;
 
     // Assigning data into object
     obj.Name = str;
     obj.Age = age;
     obj.Ratings = ratings;
 
     // Writing the object's data in file
     file_obj.write(( char *)&obj, sizeof (obj));
 
     // Feeding appropriate data in variables
     str = "Terry" ;
     age = 21;
     ratings = 3200;
 
     // Assigning data into object
     obj.Name = str;
     obj.Age = age;
     obj.Ratings = ratings;
 
     // Writing the object's data in file
     file_obj.write(( char *)&obj, sizeof (obj));
 
     return 0;
}
 
// Function definition of output_highest_rated() to
// extract info from file Data Base
int Contestant::output_highest_rated()
{
     // Object to read from file
     ifstream file_obj;
 
     // Opening file in input mode
     file_obj.open( "Input.txt" , ios::in);
 
     // Object of class contestant to input data in file
     Contestant obj;
 
     // Reading from file into object "obj"
     file_obj.read(( char *)&obj, sizeof (obj));
 
     // max to store maximum ratings
     int max = 0;
 
     // Highest_rated stores the name of highest rated contestant
     string Highest_rated;
 
     // Checking till we have the feed
     while (!file_obj.eof()) {
         // Assigning max ratings
         if (obj.Ratings > max) {
             max = obj.Ratings;
             Highest_rated = obj.Name;
         }
 
         // Checking further
         file_obj.read(( char *)&obj, sizeof (obj));
     }
 
     // Output is the highest rated contestant
     cout << Highest_rated;
     return 0;
}
 
// Driver code
int main()
{
     // Creating object of the class
     Contestant object;
 
     // Inputting the data
     object.input();
 
     // Extracting the max rated contestant
     object.output_highest_rated();
 
     return 0;
}

输出如下:

Terry

被认为是行业中最受欢迎的技能之一, 我们拥有自己的编码基础C ++ STL通过激烈的问题解决过程来训练和掌握这些概念。

木子山

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: