从文件中读取对象并打印

reading object from file and printing it

本文关键字:打印 取对象 读取 文件      更新时间:2023-10-16

我正在写一个关于从文件读取/写入的项目,当我试图从文件读取对象时也会遇到同样的问题。我必须打印所有成绩>3.00的学生,但它什么都不打印!有什么建议吗?抱歉我英语不好;)这是我的代码:

#include<iostream>
#include<fstream>
#include<cstring>
#include<algorithm>
#include<queue>
const int  MAX_SIZE = 35;
using namespace std;
class Student {
char name[MAX_SIZE];
double mark;
int phone_number;
public:
Student(char const* _name = "anonimous", double = 2, int = 0);
char const* getName() const { return name; }
int getMark() const { return mark; }
int getPhone() const { return phone_number;}
void setMark(double newmark) { mark = newmark; }
void setPhone(int newphone) { phone_number = newphone; }

friend istream& operator>>(istream& is, Student& s);
friend ostream& operator<<(ostream& os, Student const& s);
bool operator>(Student const& s) const;
bool operator<(Student const& s) const;
void mycopy(Student const& s){
strcpy(name,s.getName());
mark = s.getMark();
phone_number = s.getPhone();
}
};

Student::Student(char const* _name, double _mark, int _phone_number)
  :mark(_mark), phone_number(_phone_number){
     strncpy(name, _name, MAX_SIZE);
     name[MAX_SIZE] = '';
}
istream& operator>>(istream& is, Student& s){
       return (is >> s.phone_number >> s.mark).getline(s.name,MAX_SIZE);
}
ostream& operator<<(ostream& os, Student const& s){
return os << "Name: "<<s.name<<" Grade: "<<s.mark<<" Phone: "<<s.phone_number<<endl;
 }
 bool Student::operator>(Student const& s) const{
 return name > s.getName();
 }
bool Student::operator<(Student const& s) const{
 return name < s.getName();
 }
Student* readStudent(int n){
   Student* s = new Student[n];
   for(int i=0;i<n;i++){
         cin >> s[i];
   }
   return s;
}
void writeStudent(Student* s, int n){
   ofstream fo("my_database.txt");
   for(int i=0;i<n;i++){
         fo << s[i];
   }
}
int main(){
Student* students = new Student[10];
students[0] = Student("Ivan Petrov",4.25,359887954521);
students[1] = Student("Marina Popopva",5.75,359897254521);
students[2] = Student("Petar Ivanov",3.15,359888845723);
students[3] = Student("Stilqn Petrov",2.65,359895745812);
students[4] = Student("Ivelina Veselinova",3.20,359878745861);
students[5] = Student("Margarita Ivanova",4.50,359885421457);
students[6] = Student("Boqn Pavlov",6.00,359898632541);
students[7] = Student("Iliqn Karov",3.00,359878389699);
students[8] = Student("Ivan Dobromirov",4.18,359886574287);
students[9] = Student("Georgi Lubenov",5.61,359885749354);
writeStudent(students,10);
ifstream sf("my_database.txt");
    Student s;
    while( sf >> s){
        if (s.getMark() > 3.00){
            cout << s << endl;
        }
    }
return 0;}

有几个问题:

  • int getMark() const { return mark; }应返回double,而不是int

  • students[0] = Student("Ivan Petrov",4.25,359887954521);您的电话号码比int大,如果需要以0开头怎么办?应该是类型std::string而不是

  • 检查您的::operator>><<。它们不一致:

您的operator<<:

ostream& operator<<(ostream& os, Student const& s){
   return os << "Name: "<<s.name<<" Grade: "<<s.mark<<" Phone: "  <<s.phone_number<<endl;
}

将导致一条数据线看起来像这样:

姓名:Ivan Petrov等级:4.25电话:359887954521

然后你的operator>>:

return (is >> s.phone_number >> s.mark).getline(s.name,MAX_SIZE);

是错误的。在此过程中,您需要阅读并忽略一些字符串。我建议您使用std::string来存储名称。这里有一个假设这样使用的实现:

istream& operator>>(istream& is, Student& s){
   std::string firstName;
   std::string lastName;
   std::string tmpStr;
   is >> tmpStr; // "Name: "
   is >> firstName;
   is >> lastName;
   s.name = firstName + " " + last_name; // consider even storing these in separate fields
   is >> tmpStr; // "Grade: "
   is >> s.mark;
   is >> tmpStr; // "Phone: "
   is >> s.phone_number;
   return is;
}

其他事项:

  • Student* students = new Student[10];无需使用新的:Student students[10];

  • 尽量避免将"database.txt"硬编码为输入/输出文件。将文件名作为函数的参数。

我看到的问题:

  1. 输出和输入函数不是对称的。您有:

    istream& operator>>(istream& is, Student& s){
       return (is >> s.phone_number >> s.mark).getline(s.name,MAX_SIZE);
    }
    ostream& operator<<(ostream& os, Student const& s){
       return os << "Name: "<<s.name<<" Grade: "<<s.mark<<" Phone: "<<s.phone_number<<endl;
       // "Name: " and " Grade: " are extra data.
    }
    
  2. 当类型为std::string的变量中有空格时,operator<<()函数将把空格写入输出流,但operator>>()将停止在第一个空白字符处。因此,使用可以节省什么

    os << s.name
    

    将与您使用读取的内容不同

    is >> s.name
    

    您需要更改策略以保存名称。

    保存名称之前,请先保存名称的长度。在读取数据时,请先读取长度,然后再读取名称。

  3. 您没有保存mark。你也没有读回。

这是我对功能的建议。

istream& operator>>(istream& is, Student& s){
   // Read the size.
   size_t size;
   is >> size;
   // Read the space and discard.
   is.get();
   // Now read the name.
   is.read(s.name, size);
   // Null terminate the string.
   s.name[size] = '';
   // Read the phone number
   is >> s.mark;
   // Read the phone number
   is >> s.phone_number;
   return is;
}
ostream& operator<<(ostream& os, Student const& s){
   // Write the size.
   size_t size = strlen(s.name);
   os << size;
   // Write a space as a separator.
   os << " ";
   os.write((char*)s.name, size);
   // Write a space as a separator.
   os << " ";
   // Write the mark.
   os << s.mark;
   // Write a space as a separator.
   os << " ";
   // Write the phone number.
   os << s.phone_number;
   return os;
}