You can have as many OS dependant EOFs as you want. This hack is usefull if you are trying to trick another program that processes text files (using a text, non-binary stream).
If you are not trying to trick some existing program (that you don't have the source for) then you should do something along the lines of what elad suggested: use your own special char, which could be out of the printable range like (char)255.
More code using OS dependant EOFs:
Code:
#include <fstream>
#include <iostream>
using namespace std;
const char REAL_EOF = 26; //for DOS, use 4 for Unix
//dump contents of file 'fn'
void dump_file(const char *fn);
//replace the first occurence of an embedded EOF with the given char
void replace_first_EOF(const char *fn,char replace);
int main()
{
const char *fn = "test.txt";
ofstream out(fn);
out << "Hello World!"
<< REAL_EOF << "Text after first EOF."
<< REAL_EOF << "Text after second EOF.";
out.close();
dump_file(fn);
//output: "Hello World!"
replace_first_EOF(fn,' ');
dump_file(fn);
//output: "Hello World! Text after first EOF"
replace_first_EOF(fn,' ');
dump_file(fn);
//output: "Hello World! Text after first EOF. Text after second EOF."
return 0;
}//main
void dump_file(const char *fn)
{
ifstream in(fn);
if (!in)
return;
char c;
while (in.get(c))
cout << c;
cout << endl;
in.close();
}//dump_stream
void replace_first_EOF(const char *fn,char replace)
{
fstream in(fn,ios::in | ios::out | ios::binary);
if (!in)
return;
char c;
while (in >> c)
{
if (c == REAL_EOF)
{
//"rewind" file pointer and replace EOF
in.seekp(-1,ios::cur);
in.put(replace);
in.close();
return;
}//if
}//while
in.close();
}//replace_first_EOF
gg