html - 将c++引入html

标签 html c++ file class

所以我需要创建一个学生及其类成绩的数据库。我通过使用大量对象数组来做到这一点,但我需要将它写在像表格一样的 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[] );

然后像这样检查 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;
}

它更便于我们阅读和使用。

关于html - 将c++引入html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30731340/

相关文章:

file - 将任何文件转换为二进制文件,反之亦然

android - 从 HTML 加载图像的 Webview 问题

C++ - 什么时候函数内联是不可能的?

html - 两边有水平线的标题

C++ 项目在glewinit()之后崩溃

类内变量的 C++ 运算符重载

perl - 为什么 Perl 文件测试运算符 "-l"检测不到符号链接(symbolic link)?

java - BufferedWriter 未写入第一行文本

android - 向下滚动时触摸/单击区域在响应菜单中移位

Javascript 或 jQuery : assign value within if statement