从文件中读取对象并打印
reading object from file and printing it
我正在写一个关于从文件读取/写入的项目,当我试图从文件读取对象时也会遇到同样的问题。我必须打印所有成绩>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"硬编码为输入/输出文件。将文件名作为函数的参数。
我看到的问题:
-
输出和输入函数不是对称的。您有:
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. }
-
当类型为
std::string
的变量中有空格时,operator<<()
函数将把空格写入输出流,但operator>>()
将停止在第一个空白字符处。因此,使用可以节省什么os << s.name
将与您使用读取的内容不同
is >> s.name
您需要更改策略以保存名称。
保存名称之前,请先保存名称的长度。在读取数据时,请先读取长度,然后再读取名称。
-
您没有保存
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;
}
- 如何仅使用对象名称打印特定于对象的成员
- 如何使用gdb制作一个可以漂亮地打印每个对象的C++函数
- 打印抽象对象 c++
- 为什么平均打印数组元素比打印单个对象慢C++?
- 打印指向的对象
- 如何在 c++ 中打印存档中的对象向量
- 如何获取列表的每个对象并调用getName方法来打印其名称
- 我重载了 << 和 = 运算符。为什么当我将一个对象分配给另一个对象并尝试打印它时,我会被打印出来?
- 打印出对象向量中的每个元素C++
- 我使用箭头运算符打印出对象的变量值,并得到一些随机值作为输出
- 如何在C 中打印一个对象的2D向量
- 从对象数组打印字符串对象
- 如何使用迭代器在列表中打印类对象
- 如何在C ++中打印类对象?我已经改进了<<
- 使用for_each打印复杂对象
- C++打印出对象错误
- 如何打印地图对象
- C++打印取整
- 使用c++迭代器打印vector对象
- 如何显示/打印字符串对象?cout & lt; & lt;Int工作,count <<字符串