 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Alan Guest
|
Posted: Sun Jul 02, 2006 2:07 am Post subject: unexpected eof |
|
|
Dear All,
I'm trying to write a C++ that reads in hexadecimal characters from a
text file. For this I use the >> to read in a character at a time
inside a while loop that waits for eof to be reached. I'm able to read
in correctly about half my text file, then the program breaks out of
the while loop.
I presume this is due to the fact that one of the hex characters that
I'm trying to read in, is equals to eof. I've tried to get around this
problem, by using file pointers to determine the size of the file and
then use a for loop to read in the corresponding amount of characters.
This doing, I can copy half the initial text file into another file,
with the rest being filled with rubbish.
Does anyone have any ideas how I could get around this problem?
Thanks in advance for your help,
Regards,
Alan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Ulrich Eckhardt Guest
|
Posted: Sun Jul 02, 2006 5:06 pm Post subject: Re: unexpected eof |
|
|
Alan wrote:
| Quote: | I'm trying to write a C++ that reads in hexadecimal characters from a
text file. For this I use the >> to read in a character at a time
inside a while loop that waits for eof to be reached. I'm able to read
in correctly about half my text file, then the program breaks out of
the while loop.
I presume this is due to the fact that one of the hex characters that
I'm trying to read in, is equals to eof.
|
Hard to tell what's going on without having any first-class info. The
methodical problem with your description is that you only give use the
interpretation of the facts and not the facts themselves. What did you do?
What did you see? What did you expect to see? Those are the important
informations we need.
Other than that, there is one thing that strikes me: what is a hexadecimal
character? The point is that you describe a byte-wise copying of one file
into another, while hexadecimal character would normally mean that it is a
textfile with only a certain set of characters. BTW: operator>> by default
skips whitespace, so it's not the right tool to make a 1:1 copy of a file.
Lastly, how do you determine EOF? Typically, there is indeed an
EOF-bytevalue for files, but that shouldn't matter at all to IOStreams.
And at what position does it stop and what bytes are in the file there?
| Quote: | Does anyone have any ideas how I could get around this problem?
|
char c;
while( in.get(c))
out.write( &c, 1);
Uli
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Jul 03, 2006 1:36 am Post subject: Re: unexpected eof |
|
|
"Alan" <a.ball (AT) ulster (DOT) ac.uk> writes:
| Quote: | I'm trying to write a C++ that reads in hexadecimal characters from
a text file. For this I use the >> to read in a character at a time
inside a while loop that waits for eof to be reached. I'm able to
read in correctly about half my text file, then the program breaks
out of the while loop.
|
How do you do that? Please post minimal code and the contents of a
minimal input file that demonstrates this behavior.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
kanze Guest
|
Posted: Mon Jul 03, 2006 4:18 pm Post subject: Re: unexpected eof |
|
|
Ulrich Eckhardt wrote:
| Quote: | Alan wrote:
I'm trying to write a C++ that reads in hexadecimal
characters from a text file. For this I use the >> to read
in a character at a time inside a while loop that waits for
eof to be reached. I'm able to read in correctly about half
my text file, then the program breaks out of the while loop.
I presume this is due to the fact that one of the hex
characters that I'm trying to read in, is equals to eof.
|
[...]
| Quote: | Lastly, how do you determine EOF? Typically, there is indeed
an EOF-bytevalue for files, but that shouldn't matter at all
to IOStreams.
|
That depends. If the file has not been opened in binary mode,
input will stop when it sees the EOF character (0x1A under
Windows).
| Quote: | And at what position does it stop and what
bytes are in the file there?
Does anyone have any ideas how I could get around this problem?
char c;
while( in.get(c))
out.write( &c, 1);
|
Which will still only do a byte-wise copy of the file if: 1)
both files were opened in binary mode, and 2) both streams are
imbued with a non-mapping locale (e.g. "C").
--
James Kanze GABI Software
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
Guest
|
Posted: Sat Jul 08, 2006 4:54 am Post subject: Re: unexpected eof |
|
|
Alan wrote:
| Quote: | Dear All,
I'm trying to write a C++ that reads in hexadecimal characters from a
text file.
|
You mean, one of 0-9A-Fa-f? So characters like 'g' and \n are illegal?
For this I use the >> to read in a character at a time
| Quote: | inside a while loop that waits for eof to be reached.
|
You don't mean while .eof()==false, I hope? Because .eof() only tells
you why the last erad failed, AFTER it failed. It's not a prediction
for
the next read.
HTH,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ] |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|