简体   繁体   中英

C++ support for non-ASCII data files

I can't find any reference to how to write and read non-ASCII files in C++.

I would like to create my file format while expressing a precise pattern and markup of the informations inside the file, but basically with fstream I can only create text files. text mode or binary mode doesn't really matter for this purpose, the result is always an ASCII file.

How to write a file byte by byte like the ones that you can't simply parse with a text editor and they have their own definition ?

What you want is to treat the file as a byte stream, this can be achieved by using read() and write(). The normal stream operator << >> cannot be used when dealing directly with the file when its not text.

Normally you would create your own read/write functions on top of the read()/write() to overload the stream operators

Another approach is to use fread() fwrite() where you create a struct with the layout of a record then use that in fread()/fwrite()

typedef struct
{
   short id;
   char name[64];
} rec;

rec A;

fread( &A, sizeof(A), 1, fp );

If you have a file you can't "read with an editor", it just means that the data is not stored in text form. As others have said, C and C++ doesn't make much difference between text and binary forms of files - it's just a few simple rules about conversions (eg line endings) and conventions (eg end of file can be marked with a character, because file lengths are in blocks, but we don't want an exact multiple of 512 byte blocks in a text file, so CTRL-D or CTRL-Z is used to mark the end of thefile). In binary mode, "anything and everything goes".

In many respects, binary files are very much like text files in the respect that the compiler won't know WHAT your data represents. If a text file contains:

12345 Glurg 12.88
1Ab9Z Flarf 6.89

It would be your program that decides that the first column is the product ID (reading the first line, you'd think it's an integer, but since the second one can't be represented as an integer, it must be stored as a string), the second is the product name, and the third is a price, maybe? Or the weight? (in kilos, grams, pounds, tons?)

So, likewise for binary files, your program needs to know what each byte or collection of bytes mean.

If it's a well-known format (PDF, Excel spreadsheet, or something like that), there may be libraries available either free or for purchase, that handle that format. If not, you need a good description of the format of the file itself, and use the read/write or streambuf functionality described above.

If the format is of your own doing, or at least not uber-portable, you may be able to form structs that have the right format, and read those structs as one read operation, and write as one write operation. If the format is intended to be portable, that probably won't work - and beware that the method of reading and writing structs is not so portable, because compilers may put gaps in a struct, which varies depending on the architecture of the machine.

C++ doesn't support any binary format directly. The main reason is that there is no generally used binary format but many different ones. The stream buffers (ie, the classes derived from std::streambuf ) can be used to read bytes from an external destination or write byte to an external destination but you'd need to create a suitable set of binary formatted input and output functions.

没有对此的本地支持,如果要实现类似的功能,请使用序列化。

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