how do i zero out 1 byte

if i put cur_char=0 it puts in number 30 for byte ,how would i zero out bytes
thanks

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <fstream>
#include <cstdlib>
 
int main(  )
{
    int cur_char;   // current character to write 
    std::ofstream out_file; // output file 
 
    out_file.open("test.bin", std::ios::out);
    if (out_file.bad(  )) {
        (std::cerr << "Can not open output file\n");
        exit (8);
    }
 
    cur_char = 0; {
        out_file << cur_char;
    }
    return (0);
}

I do not understand what you are saying about. Assigning zero to an integer number as for example defined by you int cur_char; sets all its bytes to 0.
But when you output this number in a text stream it are stored as one character equal to '0'.
Last edited on
Use the proper data type, and that would be char or wchar_t if you are using wide characters. To be more specific: Make cur_char of type char. That should work. If it doesn't, then open the file in binary mode.
when you read the file i a hex editor the byte reads 30 instead it should read 00 .
If you want to output binary values to file you'll need to have the std::ios_base::binary flag on the created ofstream.
The stream inserter by default will write your numeric data into ASCII format for human readability. So, when the integer value 0 is to be written to cout, cin, a file, etc., it will be written as the character '0', which is ASCII character 48 (hex 30).

As Texan40 said, if you want the binary value written to the file, you need to use the binary flag.
it still shows 30
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <fstream>
#include <cstdlib>
 
using std::ios_base;
using std::cout;
using std::cin;
using std::endl;



int main(  )
{
    int cur_char;   // current character to write 
    std::ofstream fout("test.bin", ios_base::out | ios_base::binary | ios_base::trunc);
 
    
    if (fout.bad(  )) {
        (std::cerr << "Can not open output file\n");
        exit (8);
    }
 
    cur_char = 0; {
        fout << cur_char;
    }
    return (0);
}

hey is there another way to write 00 to a bin file other than stream?
thanks
Try fout.write(reinterpret_cast<char*>(cur_char), sizeof cur_char) ;
I said it before and I'll say it again:

1. Declare cur_char as being of type char.
2. Open the file as binary just like Texan40 stated.

The above should work just fine. DO let us know if it doesn't.
i see it finally thanks yall

typedef unsigned char cur_char; // 1byte
Topic archived. No new replies allowed.