将c ++引入html

时间:2015-06-09 11:56:11

标签: html c++ file class

所以我需要在课堂上创建学生的数据库和他的分数。我是通过使用大量的对象数组来实现的,但是我需要像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();
}

我在图书馆的课程

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

甚至没有创建文件。我做错了什么?

1 个答案:

答案 0 :(得分:1)

首先,您在路径名中使用非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;
}

我们更容易阅读并与您合作。