| 1 |
#include <iostream> |
| 2 |
#include <fstream> |
| 3 |
using namespace std; |
| 4 |
|
| 5 |
int main() |
| 6 |
{ // r/w the file |
| 7 |
fstream f("hello", ios::in | ios::out | ios::trunc); |
| 8 |
|
| 9 |
f.write("hello", 6); // write 2 NTB strings |
| 10 |
f.write("hello", 6); |
| 11 |
|
| 12 |
f.seekg(0, ios::beg); // reset to begin of file |
| 13 |
|
| 14 |
char buffer[100]; // or: char *buffer = new char[100] |
| 15 |
char c; |
| 16 |
// read the first `hello' |
| 17 |
cout << f.get(buffer, sizeof(buffer), 0).tellg() << '\n'; |
| 18 |
f >> c; // read the NTB delim |
| 19 |
|
| 20 |
// and read the second `hello' |
| 21 |
cout << f.get(buffer + 6, sizeof(buffer) - 6, 0).tellg() << '\n'; |
| 22 |
|
| 23 |
buffer[5] = ' '; // change asciiz to ' ' |
| 24 |
cout << buffer << '\n'; // show 2 times `hello' |
| 25 |
} |
| 26 |
/* |
| 27 |
Generated output: |
| 28 |
5 |
| 29 |
11 |
| 30 |
hello hello |
| 31 |
*/ |