| |

VerySource

 Forgot password?
 Register
Search
View: 894|Reply: 7

How to write information to a file in binary form

[Copy link]

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

Post time: 2020-1-13 13:20:01
| Show all posts |Read mode
I want to write unsigned long data to a file in binary, knowing the number of bits required for each data (BIT, that is, just writing N bits).
I did this but no data was written.
ifstream in ("test.txt"); // where are the characters
ofstream out;
out.open ("result.xxx", ios :: binary);

int index;

while (! in.eof ())
{
in >> index;
out.write ((char *) (code [index]), code_len [index]);
}
Reply

Use magic Report

0

Threads

19

Posts

11.00

Credits

Newbie

Rank: 1

Credits
11.00

 China

Post time: 2020-1-17 10:45:01
| Show all posts
out.write ((char *) (code [index]), code_len [index]);
----------------------------
What is code? What's in it?
Reply

Use magic Report

1

Threads

2

Posts

3.00

Credits

Newbie

Rank: 1

Credits
3.00

 China

 Author| Post time: 2020-1-20 14:45:01
| Show all posts
code is unsigned long
Reply

Use magic Report

0

Threads

24

Posts

7.00

Credits

Newbie

Rank: 1

Credits
7.00

 China

Post time: 2020-1-25 18:54:01
| Show all posts
Write a multi-byte data to a file and then recover it from the file. This method has portability problems, because different platforms have different byte ordering methods (big-endian and little-endian). A unified format (define a sequence by yourself, or use network byte order) conventions, so that can be.
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-26 00:36:02
| Show all posts
BIT, that is, just write N bits
==========
The basic unit of reading and writing files is bytes. It is not possible to write only a few bits.
Only whole bytes can be written to the file ~~~
Reply

Use magic Report

0

Threads

78

Posts

29.00

Credits

Newbie

Rank: 1

Credits
29.00

 China

Post time: 2020-1-26 10:45:01
| Show all posts
// Test it with a simple program first

char str [] = "Hello, Boy!";

ofstream out;
out.open ("result.txt", ios :: binary);
out.write (str, strlen (str)); // Write file
out.close (); // close
Reply

Use magic Report

0

Threads

11

Posts

9.00

Credits

Newbie

Rank: 1

Credits
9.00

 Invalid IP Address

Post time: 2020-1-26 12:00:02
| Show all posts
>> The landlord needs to make a convention in a unified format (define a sequence by himself or use the network byte order), so that can be done.

XDR ...
Reply

Use magic Report

0

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-2-7 07:45:02
| Show all posts
The operations on files are in bytes. The landlord does not want to operate on files in bits.
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list