patonp

I'm attempting to read through a file containing an embedded EOF character (Decimal represesentation 025). My code only reads the data until the EOF appears as my exit condition for reading the data is while (! in_file.eof() ). Are there any alternate conditions that I can use that will allow me to traverse the entire file

Thanks!

Peter



Re: Visual C++ Language Embedded EOF in File

Holger Grund

Do you talk about std::basic_ios::eof() IOStreams shouldn't treat the end-of-medium character specially. eof really only means end of stream as determined by the underlying file.

-hg





Re: Visual C++ Language Embedded EOF in File

patonp

Thanks Holger.

I also would not have thought that the end-of-medium character would have been treated specially. In fact, I've stripped down my code to the following sample.

In this sample, I'm simply reading an input file one character at a time, and writing the character to an output file. However, the program terminates where this character appears and the output file ends at that point. Is there something I'm missing

#include <fstream>

#include <iostream>

using namespace std;

int main(int argc, char **argv)

{

ifstream in_file;

ofstream out_file;

char next_char;

in_file.open(argv[1]);

out_file.open(argv[2]);

if (argc < 3)

{

cout << "2 arguments required: Please provide input file path and output path";

return -1;

}

else

{

if ( in_file.is_open() && out_file.is_open() )

{

in_file.get(next_char);

while(! in_file.eof())

{

out_file << next_char;

in_file.get(next_char);

}

in_file.close();

out_file.close();

}

else

{

cout << "Unable to open file";

if (in_file.is_open() ) in_file.close();

if (out_file.is_open() ) out_file.close();

return -1;

}

return 0;

}

}





Re: Visual C++ Language Embedded EOF in File

Holger Grund

That works in text mode and should be just fine with text mode limitation. I am not aware of anything that would directly cause an EOF on char(25) input byte with the C locale. Are you quite certain that it is really char(25) and not char(26)

Anyway, if you really want the file contents as is (including preserving CR-LF and preventing locale conversion) you open the file stream in binary mode:

in_file.open( .. , std::ios_base::in | std::ios_base::binary );

That being said, it's still not clear to me whether you really want binary mode or just want to avoid CTRL-Z handling (assuming it's really 0x1a that causes the EOF in the C Library)

-hg





Re: Visual C++ Language Embedded EOF in File

patonp

Thanks again for the quick response.

You're right about the character in question - it's a char(26), not a char(25), though I have to admit I'm not sure what this character represents.

Unfortunately, I do need to process the data in text mode, so streaming in binary mode is not an option that's available to me.

Any other options I can consider

Thanks,

Peter





Re: Visual C++ Language Embedded EOF in File

patonp

Any other suggestions about how I can handle an embedded char(26) in my file

Thanks!

Peter





Re: Visual C++ Language Embedded EOF in File

Holger Grund

I'm afraid text mode won't work for you. There's no way around the Ctrl-Z handling, AFAICT.

That being said, what's so special about text mode There's character decoding, which you probably don't use as the standard locale is used. That only leaves CR-LF folding, which you could do yourself. In fact, I think there's a filter in Boost.IOStreams, which does exactly that.

-hg





Re: Visual C++ Language Embedded EOF in File

patonp

Thanks again Holger. As you suggested, I used a binary mode read and have (so far) found it simpler than I thought to work with the data in this way.