void MyWin::OnBtnOpen()
{
QString filename = QFileDialog::getOpenFileName(this, GBK::ToUnicode("选择文件"));
if (filename.length()>0)
{
FILE *fp = fopen(GBK::FromUnicode(filename).c_str(), "rb");
fseek(fp, 0,SEEK_END);
int filesize = ftell(fp);
fseek(fp, 0, SEEK_SET);
char *buf = new char[filesize + 1];
int n=fread(buf, 1, filesize, fp);
if (n>0)
{
buf[n] = 0;
ui.plainTextEdit->setPlainText(GBK::ToUnicode(buf));
}
delete[] buf;
fclose(fp);
}
}
void MyWin::OnBtnSave()
{
QString filepath = QFileDialog::getSaveFileName(this, GBK::ToUnicode("保存"));
if (filepath.length()>0)
{
QString text = ui.plainTextEdit->toPlainText();
string gbk_text = GBK::FromUnicode(text);
string gbk_filename = GBK::FromUnicode(filepath);
FILE* fp = fopen(gbk_filename.c_str(), "wb");
fwrite(gbk_text.c_str(), 1, gbk_text.length(), fp);
fclose(fp); // 关闭文件
}
}
Qt打开文件保存文件
最新推荐文章于 2024-10-04 12:14:57 发布