Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > unexpected eof

Reply
Thread Tools

unexpected eof

 
 
Alan
Guest
Posts: n/a
 
      07-01-2006
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

 
Reply With Quote
 
 
 
 
Yong Hu
Guest
Posts: n/a
 
      07-01-2006

Alan 写道:

> 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


There are more than a few possibilities for your case. It is good if
you could show us a part of your code.

Regards,

 
Reply With Quote
 
 
 
 
Rolf Magnus
Guest
Posts: n/a
 
      07-01-2006
Alan wrote:

> 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?


We're not mind readers. You didn't show any code at all, so there is no way
to tell you what the problem might be. Your explanations are too vague.
Please post a minimal, but complete program that shows the problem.

 
Reply With Quote
 
Peteris Krumins
Guest
Posts: n/a
 
      07-01-2006
Alan wrote:
> 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?
>


You should have posted a code fragment. From your description I can't
make
a suggestion what causes the problem.

If it is a text file, then reading characters from it will never result
a character being an
eof indicator because eof indicator is different from all characters
(it is of integer type).

Instead parsing the input yourself allow C++ standard library do it.

Try the following code:

#include <iostream>
#include <fstream>
#include <iterator>
#include <algorithm>
#include <vector>
#include <cstdlib>

int main()
{
std::vector<int> hex_collection;
std::ifstream input_file("input_file.txt");

if (input_file.is_open() == false) {
std::cerr << "failed opening input_file.txt\n";
exit(EXIT_FAILURE);
}

// tell the input stream we will be reading integer values in
hexadecimal base
input_file.setf(std::ios_base::hex, std::ios_base::basefield);

std::istream_iterator<int> begin_input_iterator(input_file);
std::istream_iterator<int> end_input_iterator;

// read hex values into hex_collection vector
std::copy(begin_input_iterator, end_input_iterator,
std::back_inserter(hex_collection));

// output the values we read in.
std::copy(hex_collection.begin(), hex_collection.end(),
std:stream_iterator<int>(std::cout, " "));
std::cout << "\n";
}


P.Krumins

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Windows] Any way to distinguish ^C Induced EOF from ^Z EOF? Jan Burse Java 67 03-14-2012 12:21 AM
ifstream eof not reporting eof? SpreadTooThin C++ 10 06-15-2007 08:49 AM
Bulk insert - error: Unexpected end-of-file (EOF) encountered in data file. AB@ ASP .Net 2 04-17-2007 03:37 PM
try... except SyntaxError: unexpected EOF while parsing oscartheduck Python 7 04-04-2007 06:46 PM
if EOF = -1, can't a valid character == EOF and cause problems? Kobu C Programming 10 03-04-2005 10:40 PM



Advertisments