简体   繁体   中英

How to set the mime-type of a new created file to application/json? C++

I am creating a json string in C++ and save it to a file using fstream .

Here is the code for creating the file:

  string json="{ \"a\"= 1 }";

  fstream datei1("jsonfile.json",ios::out);
  file1 << json << endl;
  file1.close();

How could one set the mime-type to 'application/json'??

file -i jsonfile.json in linux shell gives me: jsonfile.json: text/plain; charset=utf-8 jsonfile.json: text/plain; charset=utf-8

file command try to guess the type of your file by reading it.

And read your file again: it is a plain text file. There is only a simple object stored, nothing that can lead to the application answer.

So without changing your file data, there is nothing you can do from your code to change file command answer.

From the documentation of file command :

Causes the file command to output mime type strings rather than the more traditional human readable ones. Thus it may say 'text/plain; charset=us-ascii' rather than 'ASCII text'. In order for this option to work , file changes the way it handles files recognized by the command itself (such as many of the text file types, directories etc), and makes use of an alternative 'magic' file . (See the FILES section, below).

/usr/share/file/magic.mgc Default compiled list of magic.
/usr/share/file/magic Directory containing default magic files.

You can read about magic files on the wiki .
Also you can add your own signatures in /etc/magic .

But *.json is a plain text file, without any signatures, thus, probably, it's impossible to make OS think, that some file has application/json mime type without any hacks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM