I'm using Qt for reading binary file. I read file with QFile object, but sometimes I need to go back at begining of the file and read file again, but I don't know how to do that.
You can use the seek function on your file :
file.seek(0);
If you are reading the file using a QTextStream you should use the seek function on the stream.
Related
I have an application on QT which reads from a file with text in a QByteArray format (the information belongs to a certificate stored in a file). Looks like this:
certificate="#ByteArray(0\x82\tF\x2\x1\x3\x30\x82\t\x10\x6\t*\x86H\x86\xf7\r\x1\a\x1\xa0\x82\t\x1\x4\x82\b\xfd\x30\x82\b\xf9\x30\x82\x3\xad\x6\t*\x86H\x86\xf7\r\x1\a\x1\xa0\x82\x3\x9e\x4\x82\x3\x9a\x30\x82\x3\x96\x30\x82\x3\x92\x6\v*\x86H\x86\xf7\r\x1\f\n\x1\x3\xa0\x82\x3+0\x82\x3'\x6\n*\x86H\x86\xf7\r\x1\t\x16\x1\xa0\x82\x3\x17\x4\x82\x3\x13\x30\x82\x3\xf\x30\x82\x1\xf7\xa0\x3\x2\x1\x2\x2\x1\x1\x30\r\x6\t*\x86H\x86\xf7\r\x1\x1\x5\x5\0\x30\x16\x31\x14\x30\x12\x6\x3U\x4\x3\f\vMumble User0\x1e\x17\r200130204841Z\x17\r400125204841Z0\x16\x31\x14\x30\x12\x6\x3U\x4\x3\f\vMumble User0\x82\x1\"0\r\x6\t*\x86H\x86\xf7\r\x1\x1\x1\x5\0\x3\x82\x1\xf\0\x30\x82\x1\n\x2\x82\x1\x1\0\xc6\x1d\x66\x8f\xc2\x31-N\xa1\xa0,\xf4\xb9\xc6\x80\x30)\xba\xf6\x17\x37\xec\x82\x1a\xf2s\xfc\xbc\x86\x93\x97\x83G\xccU(\vnr\t\xa8\x61\x12\b\x8f\xd6\x95T?\xc9\xe8\xe3v\x80%\xe3}\xe6,\x90\a\xe2\x18\xe0V\x80k\xee\x88&H <2G\x81\xec\xd6\xf1\xe9\xd0\xcat\x13\x9c?\x81\xcf\xaf\x14J\xc1X\xa5k\x9f\xf6P\xd9y\b\xa2\xe7\xaf\xa0l\xa0\xb4\xc7g\xc3*\xdd\x10\x16\xde\xe\xc3
Now I need generate the same format (QByteArray) using a GO application, but didn't find how generate this format.
Anyone can point me to some example or documentation on how create the same format of a QByteArray using another language (go)?
Thanks in advance.
QByteArray is just an array of bytes, in go that would just be []byte
I am working on Qt browser(code taken from Qt Tab Browser Example). This Qt browser successfully downloads an image file, but not an excel/pdf file. I need to download an excel/pdf file as attachment on click of a button. Excel generation code at back-end uses PHPExcel to generate and finally saves it using 'php://output'.
On browser side, when I read through QNetworkReply's 'readAll()' function, some encoded string '��ࡱ�' gets printed and nothing gets saved in QFile object, totally empty file.
How do I get the desired excel file from this encoded string ?
Please, any help. It is Linux OS and I use LibreOffice if it matters.
Well, got the answer. I was printing QNetworkReply object's content on the console using reply->readAll() before reading it into the file and Qt documentation states that:
QNetworkReply is a sequential-access QIODevice, which means that once
data is read from the object, it no longer kept by the device.
Therefore, i ended up with an empty file. Found this when i printed the size of the 'reply' object. Also, found some eventLoop related help from here(Roozbeh's answer).
Hope it helps someone else as well. Thanks SO !
I have read the FileWriter Filter :
" The File Writer filter can be used to write files to disc regardless of format. The filter simply writes to disc whatever it receives on its input pin, so it must be connected upstream to a multiplexer that can format the file correctly. You can create a new output file with the File Writer or specify an existing file; if the file already exists, it will be completely overwritten with the new data. "
So my question is :
I am using the FileWriter filter for writing my audio stream into the disc. Before writing the file in the disc i want to access that file , so can it be possible or should i make my own custom filter.
File writer filter does not not provide you with options to change file sharing mode while the file is being written to. Additionally, in most cases your accessing the file before it is finalized makes no sense: the files are rarely written incrementally, file finalization changes data in the middle of the file and your accessing data before the file is closed might get you bad/incomplete stream.
Roman R is right. Writers are for writing. If you need transform data - write your own Transform filter.
You can ask me directly here.
I'm using QSettings to store some settings in an INI file. However, my program is not in English, so some of the settings contain Unicode strings. It seems that Qt writes INI files not in utf8 or utf16, but in some other encoding, the string "Привет мир!" (rus. "Hello world!") looks like this:
WindowTitle=\x41f\x440\x438\x432\x435\x442 \x43c\x438\x440!
I want to edit settings file by hand, but I can't quite work with it like this. Is there a way to force Qt to save in Unicode?
Check the setIniCodec function of QSettings
Sets the codec for accessing INI files (including .conf files on Unix)
to codec. The codec is used for decoding any data that is read from
the INI file, and for encoding any data that is written to the file.
By default, no codec is used, and non-ASCII characters are encoded
using standard INI escape sequences.
So you should call it with the codec you want, eg
QSettings settings;
settings.setIniCodec("UTF-8");
Notice that you must call it immediately after creating the QSettings objects and before accessing any data.
I am creating an xml file. I need to check first if the file exists or not. If the file does not exist, create it and add the data cmg from a .cs file.
If the file exists, don't create the file just add the data cmg from a .cs file.
My code looks like this:
string filename="c:\\employee.xml";
XmlTextWriter tw=new XmlTextWriter(filename,null);//null represents
the Encoding Type//
tw.Formatting=Formatting.Indented; //for xml tags to be indented//
tw.WriteStartDocument(); //Indicates the starting of document (Required)//
tw.WriteStartElement("Employees");
tw.WriteStartElement("Employee","Genius");
tw.WriteStartElement("EmpID","1");
tw.WriteAttributeString("Name","krishnan");
tw.WriteElementString("Designation","Software Developer");
tw.WriteElementString("FullName","krishnan Lakshmipuram Narayanan");
tw.WriteEndElement();
tw.WriteEndElement();
tw.WriteEndDocument();
tw.Flush();
tw.Close();
so next time we add data to file we need to check if the file exits and add data to xml file
and as we have made empID as a primary key, if user tries to make duplicate entry we need to avoid
Is this possible to do?
if (!File.Exists(filename))
{
// create your file
}
or
if (File.Exists(filename))
{
File.Delete(filename);
}
// then create your file
File class is in System.IO namespace (add using System.IO; to your file)
You can't append records to an XML file, you have to read the file and then rewrite it.
So, just check if the file exists, and read the records from it. Then write the file including all previous records and the new record.
Have a look at the File.Exists method here
Testing for existance of a file before attempting to create it inherently is subject to a "things change after check" race condition. Who can guarantee you that your application isn't preempted and put to sleep for a moment after you checked, someone else creates/deletes that file, your app gets to run again and does exactly the opposite of what you intended ?
Windows (as well as all UN*X variants) supports file open/create modes that allow to perform that create-if-nonexistant/open-if-existant operation as a single call.
As far as .NET goes, this means for your task (create an XML file) you'd first create a System.IO.FileStream with the appropriate modes, see http://msdn.microsoft.com/en-us/library/system.io.filemode.aspx and then pass that stream to the XmlWriter constructor. That's safer than simply performing an "exists" check and hoping for the best.