#include "mainwindow.h"#include "ui_mainwindow.h"MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this); list.clear();}MainWindow::~MainWindow(){ delete ui;}void MainWindow::on_pb_Select_clicked(){ QString fileName = QFileDialog::getOpenFileName(this, "Select one file for load", "", ""); if (fileName.isEmpty()) { QMessageBox::critical(this, "Select file", "No selected file", QMessageBox::Ok); return; } QFileInfo file(fileName); qDebug() << fileName << " - " << file.filePath() << " - " << file.fileName() << " - " << file.size(); file_t temp; temp.name = file.fileName(); temp.path = file.filePath(); temp.size_name = file.fileName().toLocal8Bit().count(); temp.size = file.size(); for( int i=0; i<list.count(); i++ ) { if( list.at(i).name == temp.name ) { QMessageBox::critical(this, "Select file", "This file name is already exist!", QMessageBox::Ok); return; } } list.append(temp); updateTable();}void MainWindow::on_pb_Compress_clicked(){ FILE *file_rom = fopen("new.rom", "wb"); for( int i = 0; i < list.count(); i++ ) { FILE *file_read = fopen(list.at(i).path.toLocal8Bit(), "rb"); long size; size_t real_read; fseek(file_read, 0, SEEK_END); size = ftell(file_read); fseek(file_read, 0, SEEK_SET); // write size of current file fwrite(&list.at(i).size, sizeof(int), 1, file_rom); fwrite(&list.at(i).size_name, sizeof(int), 1, file_rom); // write name of current file std::string temp(list.at(i).name.toStdString()); const char* name_ = temp.c_str(); int tt = temp.size(); qDebug() << "name_=" << name_ << "tt=" << QString::number(tt); fwrite(name_, sizeof(char), tt, file_rom); char buff[size]; real_read = fread(buff, sizeof(char), size, file_read); fwrite(buff, sizeof(char), real_read, file_rom); fclose(file_read); } fclose(file_rom); std::string temp1(list.at(0).name.toStdString()); const char* name_1 = temp1.c_str(); selectFile(name_1);}void MainWindow::updateTable(){ ui->tableWidget->clear(); ui->tableWidget->setRowCount( list.count() ); for( int row = 0; row < ui->tableWidget->rowCount(); row++ ) { for( int column = 0; column < ui->tableWidget->columnCount(); column++ ) { ui->tableWidget->setItem(row, column, new QTableWidgetItem); ui->tableWidget->item( row, column )->setTextAlignment(Qt::AlignCenter); } ui->tableWidget->item(row,0)->setText( list.at(row).name ); ui->tableWidget->item(row,1)->setText( QString::number(list.at(row).size) ); }}int MainWindow::selectFile(const char *name){ FILE *file_read = fopen("new.rom", "rb"); fseek(file_read, 0, SEEK_SET); size_t real_read; int size_file; int size_name; real_read = fread(&size_file, sizeof(int), 1, file_read); real_read = fread(&size_name, sizeof(int), 1, file_read); char read_name[size_name];// real_read = fread(read_name, sizeof(char), size_name, file_read); real_read = fread(read_name, sizeof(read_name), 1, file_read); fclose(file_read); qDebug() << "size_name=" << size_name << " real_read=" << real_read << " read_name=" << read_name << " name=" << name; if( read_name == name ) qDebug() << "ok"; else qDebug() << "err"; return -1;}
char read_name[size_name];
C++ (Qt) char read_name[size_name + 1] = { 0 };
C++ (Qt)char read_name[size_name + 1] = { 0 };real_read = fread(read_name, sizeof(read_name) - 1, 1, file_read);// real_read = fread(read_name, size_name, 1, file_read); // или так
C++ (Qt)const char * str = "abc";