从文件中读取对象并打印它

时间:2015-12-28 21:37:03

标签: c++

我正在写一个关于从文件读/写的项目,当我尝试从文件中读取一个对象时遇到同样的问题。我必须打印所有等级为>的学生。 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] = '\0';
}

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;}

2 个答案:

答案 0 :(得分:2)

有几个问题:

  • 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];

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

答案 1 :(得分:0)

我看到的问题:

  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。你也不是在读它。

  4. 这是我对功能的建议。

    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] = '\0';
    
       // 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;
    }