-
c++ eof chrachter
Hi all
I need a some help inserting a c++ end of file character in a(.txt) file so that when some one comes to read the file he will reach the virtual end before reaching the physical end of the file, the eof charachter should be inserted at the first white space.
(what is the character and how to insert it
and how to reset the flags after i reach an eof)
Thanks
-
EOF cannot be represented as a char, as its value is outside of the given range.
You will need to "invent" your own eof type character, something in the range of 0x00 to 0xFF that will not be used for anything in your file except denoting a virtual eof marker.
But why does this anyway?
-
Code:
while (!eof())
//do stuff
:confused:
-
Code:
ofstream outFile;
...
outFile << EOF;
perhaps?
-
Quote:
Originally posted by Jamsan
Code:
while (!eof())
//do stuff
:confused:
No. Also, that has nothing to do with outputing an EOF character.
outFile << EOF;
Read my reply. EOF is not valid in char format.
-
havent got it yet
Hello here id the code i tried
#include <fstream.h>
#include <stdio.h>
void main()
{
ofstream SaveFile("test.txt");
SaveFile <<"I should see this when i open the file with note pad, "<<EOF<< " I should not see this text";
SaveFile.close();
ifstream OpenFile("test.txt");
char ch;
while(!OpenFile.eof())
{
OpenFile.get(ch);
cout << ch;
}
OpenFile.close();
}
It didnt work
I guess the missing link is what to output instead of EOF
i tried (-1,00Xff)
I guess i just need to stuff a badbit some where but what is a bad bit and how to stuff it
Thanks a lot for your time
-
hamada: You need to actually read what I've already posted. :(
And read this too.
-
Ok, here's how you do it, allthough it isn't portable. The EOF "character" in DOS (or windows for that matter) is Cntrl-Z, 0x1A or 26. For most Unix style machines, it's Cntrl-D, 0x04, or 4.
If you run this on a windows machine, you will get the desired effect:
Code:
#include <fstream>
#include <iostream>
using namespace std;
int main()
{
ofstream out("test.txt");
out << "Hello World!" << (char)26 << " This is EOF";
out.close();
ifstream in("test.txt");
while (!in.eof())
{
char c;
in.get(c);
cout << c;
}//while
return 0;
}//main
This will output "Hello Word!". The file test.txt will have all the text we inserted (notepad.exe will show the EOF character as a block). You should note that this trick will only work on non-binary input streams (which is why notepad has no problem reading the whole file).
The EOF constant in C/C++ is -1, which is why that don't work.
gg
-
As far as I know files can only have one EOF and it is put there automatically, you can't do it at will. You can design your code to stop writing to file or reading from file at will, that is at a given flag, which could be any valid symbol or group of symbols.
for example, say you never will have a tilde in the useable data contained in a file. Then you can use a tilde to act as a flag.
Code:
ofstream fout("filename.ext");
char data[] = "Save up to the tilde. ~ Not after.";
for(int i = 0; i < strlen(data); ++i)
{
if(data[i] != '~')
fout << data[i];
}
and the reverse if you have created a file in some other program and want to read it in to the current program up to a given flag.
Code:
ifstream fin("filename.ext")
char input[100];
char ch;
int index = 0;
fin >> ch;
while(ch != '~' && fin)
{
input[index++] = ch;
fin >> ch;
}
input[index] = '\0';
there are other ways of accomplishing this using different types of flags, etc. , but this works. To my knowledge, however, you can not have more than one EOF per file and you can't manipulate where it is.
-
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
-
Thanks for all of you
Thanks guys for all your help. Although i was trying to force notepad to recognize the virtual eof but i think that is impossible and what have been done untill know is the maximum we can reach.
Thanks for all your effort it have been helpfull