How to detect an image file is svg-fomat strictly? - c++

QFile file(filepath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "DetectImageFormat() failed to open file:" << filepath;
return "";
}
const QByteArray data = file.read(1024);
// Check svg file. but sometimes i get the data failed!
if (data.indexOf("<svg") > -1) {
return "svg";
}

Related

QFile. Device not open

I have problem with QFile.
QFile file1("file1.dat");
QFile file2("file2.dat");
if(file2.exists())
{
}
if(!file1.open(QIODevice::ReadOnly))
{
qDebug() << "Ошибка открытия для чтения";
}
if(!file2.open(QIODevice::WriteOnly))
{
qDebug() << "Ошибка открытия для записи";
}
QByteArray block = file1.readAll();
file2.write(block);
file1.close();
file2.close();
ERROR:
QIODevice::read (QFile, "file1.dat"): device not open
try to open file1.dat in read-write mode:
if(!file1.open(QIODevice::ReadWrite))
{
qDebug() << "Ошибка открытия";
}
Because if you open it just for reading, it cannot be created if it doesnt exist, or create it manually at first.
and in case that file is not opened you are not doing anything, so just for being sure check if both files were opened at first:
if(file1.isOpen() && file2.isOpen()){
QByteArray block = file1.readAll();
file2.write(block);
file1.close();
file2.close();
}

how to purposely fail "fopen"

I need to purposely fail the "fopen"-ing of a file to test my code. How do I do that. My code so far:
void MeshTrian::init (std::string filename)
{
if (!sofa::helper::system::DataRepository.findFile(filename))
{
msg_error() << "File '" << filename << "' not found." ;
return;
}
FILE *f = fopen(filename.c_str(), "r");
bool status;
msg_error_when(!(status=f))<<sofa::helper::message::UnableToOpenFile(filename.c_str());
if (status)
{
readTrian (f);
fclose(f);
}
}

Why is the QByteArray read from a file smaller than the newly downloaded QByteArray?

I am attempting to compare a QByteArray of an already saved html file with a QByteArray that was just downloaded. I have to convert the QString of the file's contents to QByteArray in order to compare them (or vice versa) and comparing bytes seems like the cleanest method, however when converted from QString to QByteArray, the size of the new QByteArray is smaller than what it should be. QByteArray QString::toLocal8Bit() const states that if it is undefined, the characters will be suppressed or replaced. It also said that it uses toLatin1() by default and tried to use ASCII since that is what a website is encoded in. I still get the same results.
bool NewsBulletin::compareDownload(QByteArray new_contents, QString filename)
{
bool return_what = false;
qDebug() << "I am in compareDownload";
// qDebug() << new_contents[1];
// qDebug() << new_contents[1] << endl
// << new_contents[2];
QFile file(application_path + filename);
if (file.exists())
{
// QString new_contents_qstr(new_contents);
file.open(QIODevice::ReadOnly | QIODevice::Text);
QTextStream in(&file);
QTextCodec::setCodecForLocale(QTextCodec::codecForName("ASCII"));
QString file_contents = in.readAll();
QByteArray file_byte_array = file_contents.toLocal8Bit();
qDebug() << "outputting new file array";
qDebug() << new_contents[5] << new_contents.size();
qDebug() << "outputting old file array";
qDebug() << file_byte_array[5] << file_byte_array.size();
for (int i=0; i<=file_byte_array.size(); i++)
{
if (file_byte_array[i] != new_contents[i])
{
return_what = true;
break;
}
else if (i == file_byte_array.size())
{
qDebug() << "compareDownload will return false, duplicate file.";
return_what = false;
}
}
}
else
{
qDebug() << "compareDownload will return true, DNE.";
return_what = true;
}
return return_what;
}
The output of the qDebug() from the function is:
I am in compareDownload
outputting new file array
T 64704
outputting old file array
T 64576
After reading the api for hours, I found the reason for the bytes being different.
file.open(QIODevice::ReadOnly | QIODevice::Text);
QIODevice::Text needs to be removed. This flag changes end of line terminators into the terminator for cpp, "\n", thus giving a byte difference.

qt binary file writing and reading

void write(QString filename) {
QChar ch('b');
QFile mfile(filename);
if (!mfile.open(QFile::WriteOnly) {
qDebug() << "Could not open file for writing";
return;
}
QDataStream out(&mfile);
out.setVersion(QDataStream::Qt_4_8);
out << ch;
mfile.close();
}
open binary file and writing 'b'(binary)
void read(QString filename) {
QFile mfile(filename);
if (!mfile.open(QFile::ReadOnly)) {
qDebug() << "Could not open file for reading";
return;
}
QDataStream in(&mfile);
in.setVersion(QDataStream::Qt_4_8);
QChar mT;
in >> mT;
qDebug() << mT;
mfile.close();
}
read but not mT='b'.if ch and mT variables are int always mT=4 why?How can i writing ch(binary file) and read from binary file
the 4 number is the length of your data. QDataStream store length of your data before it to indicate how many bytes need to read to gain the written data. your data has been written after it.

Rename file if button is pressed in TreeView Qt

I have a treeview that displays a folder with text files. There is a 'open' button. That will open the file. But when this button is pressed it should rename the file to: read filename.txt. So if there is a file for example that is name nameslist.txt and the button is pressed it should rename it to read nameslist.txt or something similar. I thought of something like this:
void berichtenhistorie::on_Openbutton_released()
{
QModelIndex index = ui->treeView->currentIndex();
QString name = index.fileName();
QString path = index.filePath();
QFile file(path);
file.open(QIODevice::WriteOnly | QIODevice::Text);
file.rename("read " + name);
file.close();
}
But this isnt working. I get the following error's:
error: C2352: 'QDirModel::fileName' : illegal call of non-static member function
But I dont know how to use fileName() and filePath() correctly.
Thanks for help!
I think that here is what you looking for:
void berichtenhistorie::on_Openbutton_released()
{
QModelIndex index = ui->treeView->currentIndex();
QFileSystemModel *model = (QFileSystemModel*)ui->treeView->model();
QString path = model->filePath(index);
QString name = model->fileName(index);
QString dir = path;
dir.remove(dir.size() - name.size(), name.size());
QFile file(path);
if(file.open(QIODevice::WriteOnly | QIODevice::Text))
{
//Interact with the file
file.close();
if(file.rename(QString("%1read %2").arg(dir, name)))
qDebug() << "Renamed";
}
}
Here's a break out of each step you'll need to do to work with QFile.
QFile file("test.txt");
if(file.exists())
{
qDebug() << "found file";
if(file.open(QIODevice::ReadWrite))
{
qDebug() << "opened";
if(file.rename("text1.txt"))
{
qDebug() << "renamed";
}
else
{
qDebug() << "failed to rename";
}
file.close();
}
}
else
{
qDebug() << "file does not exist";
}
In the end, you'll only really need your debugger to step through instead of printing out everything known to your application. E.g.
QFile file("test.txt");
if(file.exists() && file.open(QIODevice::ReadWrite))
{
if(file.rename("text1.txt"))
{
qDebug() << "renamed";
}
file.close();
}