Read/write one byte

Hello, I am very new to C++. My question is how you read and write one single byte to /from a file?

(1) So say I wanted to put the third byte to FF (in hexdecimal), how do I do that?
(2) If I wanted to read the third byte and print out the ascii value for that byte, how do I do that?

Thanks.
1) seek to the position and write

1
2
3
4
5
6
7
8
#include <fstream>
int main()
{
    std::ofstream f("test.bin");
    f.seekp(2);
    unsigned char b = 0xff;
    f << b;
}


2) seek to the position and read
1
2
3
4
5
6
7
8
9
10
#include <iostream>
#include <fstream>
int main()
{
    std::ifstream f("test.bin");
    f.seekg(2);
    unsigned char b;
    f >> b;
    std::cout << std::hex << +b << '\n';
}

Excellent! That was exactly what I needed. Thank you!
Topic archived. No new replies allowed.