所以我需要创建一个学生和他在课堂上的分数的数据库。我通过使用大量的对象数组来实现,但我需要像表一样将其写入html文件中,并创建了特殊的函数save。
#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();
}
my class in library
#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字符。我从来没有试过这样做,所以我不能帮你但也许你可以试试这里的答案打开文件与非ASCII字符
你写文件的方式不对。我假设您试图将所有学生写入同一个文件,但不要使您的文件成为全局变量,而是将其传递给这样的函数。
void save( std::ofstream &file, Student KI[] );
检查main中的内容,比如
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;
}
这对我们来说更容易阅读,对你来说更容易使用