因此,我需要创建一个学生及其班级成绩的数据库。我通过使用大量对象数组来完成此操作,但是我需要像表一样将其写入html文件中,并创建特殊的功能保存。
#include <iostream>
#include "windows.h"
#include <fstream>
#include "TkachenkoLab.h"
using namespace std;
void save(Student KI[]);
ofstream file_out("C:\\Users\\ТКаченко\\Desktop\\МП ЛАБ №7\\LAB7\\page.html");
int main()
{
SetConsoleCP(::GetACP());
SetConsoleOutputCP(::GetACP());
short n;
cout << "Ââåä³òü ê³ëüê³ñòü ñòóäåíò³â ãðóïè: ";
cin >> n;
cin.clear(); cin.sync();
cout << "\n —-— ÑÒÂÎÐÅÍÍß ÃÐÓÏÈ Ê² —---\n";
Student KI[n];
cout << "\n —-— ÑÏÈÑÎÊ ÃÐÓÏÈ Ê² —---\n";
short i;
for (i = 0; i < Student::cnt(); i++ )
cout << i+1 << ". " << KI[i].name << endl;
cout << "\n —-— reading student —-— \n";
for (i = 0; i < Student::cnt(); i++ )
KI[i].in_res();
save(KI);
if (file_out.is_open())
file_out.close();
return 0;
}
void save(Student KI[])
{
file_out.open("C:\\Users\\ТКаченко\\Desktop\\МП ЛАБ №7\\LAB7\\page.html",ios::trunc);
file_out << "<html>" << endl;
file_out << "<head>" << endl;
file_out << "</head>" << endl;
file_out << "<body>" << endl;
file_out << "<table class=\"simple-little-table\">" << endl;
file_out << "<tr>" << endl;
file_out << "<td>студент</td>" << endl;
file_out << "<td>мп</td>" << endl;
file_out << "<td>кс</td>" << endl;
file_out << "<td>физра</td>" << endl;
file_out << "<td>средний бал</td>" << endl;
file_out << "</tr>" << endl;
for (short i = 0; i < Student::cnt(); i++ )
{
file_out << "<td>студент</td>" << endl;
file_out << "<td>"<<KI[i].name<<"</td>" << endl;
file_out << "<td>"<<KI[i].MP<<"</td>" << endl;
file_out << "<td>"<<KI[i].KC<<"</td>" << endl;
file_out << "<td>"<<KI[i].fiz_ra<<"</td>" << endl;
file_out << "</tr>" << endl;
}
file_out << "</table>" << endl;
file_out << "</body>" << endl;
file_out << "</html>" << endl;
file_out.close();
}
我在图书馆上课
#include <string>
using namespace std;
class Student
{
public:
Student();
static short cnt() { return cnt_stud; };
void in_res();
void out_res();
string name;
~Student() {};
short MP, KC, fiz_ra;
static short cnt_stud;
short s_bal () { return (short)(MP+KC+fiz_ra)/3; };
};
void Student::out_res()
{
}
Student::Student()
{
cout << "ПІБ студента: ";
getline(cin, name);
MP = 0;
KC = 0;
fiz_ra = 0;
cnt_stud++;
};
short Student::cnt_stud = 0;
void Student::in_res()
{
cout << name << ": ";
cin >> MP; cin >> KC; cin >> fiz_ra;
}
它甚至都没有创建文件。我做错了什么?
最佳答案
首先,您在路径名中使用非ASCII字符。我从来没有尝试过这样做,所以我在那里不能为您提供帮助,但是也许您可以在这里尝试答案Open File with Non ASCII Characters
您做错了,这是您写入文件的方式。我假设您正在尝试将所有学生写入同一文件,但不要将文件设为全局变量,而是将其传递给这样的函数。
void save( std::ofstream &file, Student KI[] );
然后像这样检查主要内容
int main() {
std::ofstream file( "PathOfFile" );
if ( file.is_open() ) {
// Do what you want to do with your file
save( file, student );
file.close();
}
else {
std::cout << "Failed to open file" << std::endl;
}
return 0;
}
对我们来说,阅读和与您合作更容易。