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 30 31
|
#include <iostream>
#include <fstream>
using namespace std;
int main()
{ // r/w the file
fstream f("hello", ios::in | ios::out | ios::trunc);
f.write("hello", 6); // write 2 NTB strings
f.write("hello", 6);
f.seekg(0, ios::beg); // reset to begin of file
char buffer[100]; // or: char *buffer = new char[100]
char c;
// read the first `hello'
cout << f.get(buffer, sizeof(buffer), 0).tellg() << '\n';
f >> c; // read the NTB delim
// and read the second `hello'
cout << f.get(buffer + 6, sizeof(buffer) - 6, 0).tellg() << '\n';
buffer[5] = ' '; // change asciiz to ' '
cout << buffer << '\n'; // show 2 times `hello'
}
/*
Generated output:
5
11
hello hello
*/
|