Reading bytes from binary in long int

I have two questions:

  • I have binary data. I want to read the first 8 bytes for a signed long int using a read function, but I could not. Do you know how I can do this?

  • How can I directly read a data block into a string? Can I read as shown in ex:

    ifstream is; is.open ("test.txt", ios::binary ); string str ; is. read ( str.c_str, 40 ) ; // 40 bytes should be read 
+4
source share
3 answers

I want to read the first 8 bytes for a signed long int using a read function, but I could not. Do you know how I can do this?

Do not assume that long is wide enough, often not. long long guaranteed to have a width of at least 8 bytes, although:

 long long x; is.read(static_cast<char *>(&x), 8); 

Remember that this is still incredibly unbearable due to different integer sizes and judgments.

Regarding your second question, try

 char buf[41]; is.read(buf, 40); // check for errors buf[40] = '\0'; std::string str(buf); 

or safer

 char buf[41]; is.get(buf, sizeof(buf), '\0'); std::string str(buf); 
+3
source

I am sure that you mean 8 bytes in a 64-bit integer, and there are many ways to do this. One way is to use union :

 union char_long { char chars[8]; uint64_t n; }; // Extract 8 bytes and combine into a 64-bit number by using the // internals of the union structure. char_long rand_num; for(int i = 0; i < 8; i++) { rand_num.chars[i] = in.get(); // `in` is the istream. } 

Now rand_num.n will have an integer so you can access it.

Regarding the second question. Read in bytes and assign them to a string:

 const int len = 5; // Some amount. char *buf = new char[len]; ifstream in("/path/to/file", ios::binary); in.read(buf, len); string str; str.assign(buf); delete[] buf; 
0
source

You may be worried about code and data portability: if you exchange binary files between different machines, binary data will be considered garbage (for example, due to differences in size and word differences). If you are only reading binary data on the same computer that wrote it, this is normal.

Another problem, especially when the data is huge and / or expensive, is reliability in relation to the evolution of your code base. For example, if you are reading a binary structure, and if you had to change the type of one of your fields from int (or int32_t ) to long (or int64_t ), then your binary data file is useless (unless you code specific conversion procedures). If the binary was expensive to produce (for example, an experimental device or expensive computation is required to create one), you may have problems.

This is why structured text formats (which are not a silver bullet but useful) or database management systems are used. Structured text formats include XML (which is rather complicated), Json (which is very simple) and Yaml (the complexity and power between XML and Json). And text formats are easier to debug (you can look at them in the editor). There are several free libraries for working with these data formats. Databases are often more or less relational and Sql . There are several free DBMS programs (for example, PostGresQL or MySQL ).

It refers to portability of binary files (between different machines), which may interest you in serialization methods, formats ( XDR , Asn1 ) and libraries (for example, S11n and others).

If space or bandwidth is a concern, you can also consider compressing your text data.

0
source

Source: https://habr.com/ru/post/1379769/


All Articles