[英]save and load c++ program
Ok so I figured out and learned a lot of stuff today and I want to thank the community for that. 好的,所以今天我想出了很多东西,我想感谢社区。 I haven't had any bump in the roads for a few hours now but now I'm stuck.
我现在几个小时没有在道路上碰到任何撞击但是现在我被卡住了。
The last bump in the road. 路上的最后一个颠簸。 Saving and Loading my program.
保存并加载我的程序。 I have no idea where to start.
我不知道从哪里开始。 I looked at how fwrite... and fread... works and all the examples are for programs that aren't split.
我看了fwrite ...和fread ......是如何工作的,所有的例子都是针对那些没有拆分的程序。 I don't know where to start with my files.
我不知道从哪里开始我的文件。 I'll put up 2 functions.
我会提出2个功能。 If someone can help me how to do save those I can probably figure out the rest.
如果有人可以帮助我如何保存那些我可以理解其余部分。
in gradebook.h 在gradebook.h
class Student {
public:
string last;
string first;
int student_id;
};
class Course {
public:
string name;
int course_id;
vector <Student> students;
};
class Gradebook {
public:
Gradebook();
void addCourse();
void addStudent();
private:
vector <Course> courses;
};
in gradebook.cpp 在gradebook.cpp
void Gradebook::addCourse() {
int i, loop=0;
cout << "Enter Number of Courses: ";
cin >> loop;
for(i=0; i<loop; i++) {
//create newEntry to store variables
Course newEntry;
cout << "Enter Course ID: ";
cin >> newEntry.course_id;
cout << "Enter Course Name: ";
cin >> newEntry.name;
//set variables from newEntry in Courses
courses.push_back(newEntry);
}
}
void Gradebook::addStudent() {
int i, loop=0;
cout << "Enter Number of Students: ";
cin >> loop;
for(i=0; i<loop; i++) {
//create newEntry to store variables
Student newEntry;
cout << "Enter Student ID: ";
cin >> newEntry.student_id;
cout << "Enter Last Name: ";
cin >> newEntry.last;
cout << "Enter First Name: ";
cin >> newEntry.first;
//set variables from newEntry in Students
courses[0].students.push_back(newEntry);
}
}
So if a user was to input some variables in courses and students how would i use fwrite... to save the data? 因此,如果用户要在课程和学生中输入一些变量,我将如何使用fwrite ...来保存数据?
I wouldn't recommend fwrite
, instead look into <fstream>
. 我不推荐使用
fwrite
,而是查看<fstream>
。 ifstream
, ofstream
ifstream
, ofstream
Basic saving: 基本储蓄:
ofstream out("data.txt"); //save file data.txt
out << thedata; //use the << operator to write data
Basic loading: 基本装载:
ifstream in("data.txt"); //reopen the same file
in >> thedata; //use the >> operator to read data.
Here's some sample code that might help without solving the whole thing for you. 这里有一些示例代码可能会帮助您解决整个问题。
#include<iostream>
#include<string>
#include<vector>
#include<fstream>
class Student {
public:
Student()
: student_id(0)
{
}
Student(const std::string &f, const std::string &l, int id)
: first(f)
, last(l)
, student_id(id)
{
}
std::string last;
std::string first;
int student_id;
};
std::ostream &operator <<(std::ostream &os, const Student &s)
{
os << s.last << '\t'
<< s.first << '\t'
<< s.student_id << '\t';
return os;
}
std::istream &operator >>(std::istream &is, Student &s)
{
is >> s.last
>> s.first
>> s.student_id;
return is;
}
bool WriteIt(const std::string &sFileName)
{
std::vector<Student> v;
v.push_back(Student("Andrew", "Bogut", 1231));
v.push_back(Student("Luc", "Longley", 1232));
v.push_back(Student("Andrew", "Gaze", 1233));
v.push_back(Student("Shane", "Heal", 1234));
v.push_back(Student("Chris", "Anstey", 1235));
v.push_back(Student("Mark", "Bradtke", 1236));
std::ofstream os(sFileName);
os << v.size();
for (auto s : v)
os << s;
return os.good();
}
bool ReadIt(const std::string &sFileName)
{
std::ifstream is(sFileName);
int nCount(0);
is >> nCount;
if (is.good())
{
std::vector<Student> v(nCount);
for (int i = 0; i < nCount && is.good(); ++i)
is >> v[i];
if (is.good())
for (auto s : v)
std::cout << s << std::endl;
}
return is.good();
}
int main()
{
const std::string sFileName("Test.dat");
return !(WriteIt(sFileName) && ReadIt(sFileName));
}
Bonus points if you recognise who my "students" are. 如果你认识我的“学生”是谁,你可以获得奖励积分。 :-)
:-)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.