Nominate our 2022 Qt Champions!

How to write metadata to a file and read it again?

  • I want to develop application for monitoring the changes occurred on a folder and all its sub-folders which include moving, deleting, copying, deleting and adding (by saving or adding from a place out the main folder).
    And I want to use metadata to store information about the file (history and relation to the others).
    I see that there is a class called QMetaDataReaderControl is that what I need? if "yes" how to do that. Could any one give me a simple example.

    Thanks in advance.

  • For the first part, monitoring file/directory changes, a QFileSystemWatcher is what you need.

    But what exactly do you mean with "meta data" here ?

    BTW: The QMetaDataReaderControl class provides read access to the meta-data of a QMediaService's media

  • Never mind metadata, I want to append information to the file so I could depend on them when the file moved to different place.
    But if you know about software like Adobe Bridge you could in a Bridge browser to right click and choose "File Info..." and you will get information related to the file like "Document Title", "Author", "Description"...

    About QFileSystemWatcher, How many folders could I watch (the main folder and all his sub-folders)?

  • Append information to a file? Simply use QFile and open the file with QIODevice::Append flag :)

    As for meta data: It has a meaning on the File System level, where "meta data" refers to everything that is stored in the file system data structures, but is not the file's content. This would be the file's name, the file's size, the file attribute flags (e.g. "read only"), the list of data blocks assigned to that file, and so. Though, it seems you are talking about "meta data" like it's used for media files, e.g. "title", "artist", "album" and so on. That's actually data that is stored in the file. And it of course is specifc to the individual File Format! For example, the MP3 format does not have support for "meta data" by itself, but people have invented the ID3 Tag to workaround this limitation. Other file formats have other "ways" to embed meta information. So I don't think Qt has a class that can do it for any file format. You need to look up in the individual specifications of the individual file format how meta data, if support, has to be embedded. Then just use QFile, maybe with QDataStream, to write the data into the file...


    About your QFileSystemWatcher question, the docs say this:

    The act of monitoring files and directories for modifications consumes system resources. This implies there is a limit to the number of files and directories your process can monitor simultaneously. On Mac OS X 10.4 and all BSD variants, for example, an open file descriptor is required for each monitored file. Some system limits the number of open file descriptors to 256 by default. This means that addPath() and addPaths() will fail if your process tries to add more than 256 files or directories to the file system monitor. Also note that your process may have other file descriptors open in addition to the ones for files being monitored, and these other open descriptors also count in the total. Mac OS X 10.5 and up use a different backend and do not suffer from this issue.

  • If I try to use QFile and open the file with QIODevice::Append flag, is this will corrupt the file and would be not available in it's application?

  • [quote author="dulajun" date="1369523401"]If I try to use QFile and open the file with QIODevice::Append flag, is this will corrupt the file and would be not available in it's application? [/quote]

    I don't understand that sentence.

    If you open a file with QFile and QIODevice::Append, it does nothing but opening the file in append mode. It does not corrupt anything. It only gives you the ability to append additional data to that file in the following. It's your obligation to append "meaningful" data to the file in the subsequent write() operations.

    Of course this is completely File Format specific...

  • Like if I open a jpg or photoshop file and append some information. Then after closing the photoshop file will be available in Photoshop application and the application will keep the same information after saving the file?

  • I still don't really understand.

    Anyway: If you open a JPEG file to append or modify data, it should be clear that this requires a detailed understanding of the JPEG file format. That's because, if you append/modify an existing JPEG file, the new content of the file must be a valid JPEG file (according to the JPEG specification) again. As long as you make that sure, Photoshop (or whatever app that can read JPEG files) will be able to read the "modified" file. Otherwise not.

    As for JPEG files and "meta information", I think Exif is what you are looking for:

  • May I suggest instead a database or file that is always accessible to your application containing "metadata" about files that your application is monitoring.

  • I want to write information which are File ID, File Name and File Path.
    And want these information to be stuck with the file, so I can compare the actual name and path with the appended name and path and determine where the file was.

  • Could you MuldeR please help me in modifying this snippet of code to append a text to the end of the file and then read that text from the end of the same file. The text has a length of 1024 unicode characters exactly.

    // append 1024 letters
    QFile myFile("/Users/dulajun/Documents/pages.indd");;
    QDataStream out(&myFile);
    //Here the code

    //read from the end 1024 letters;
    QDataStream in(&myFile);
    QString text; //to receive the text.
    //Here the code

Log in to reply