473,503 Members | 5,004 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

Jul 1 '06 #1
3 3510

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,

Jul 1 '06 #2
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.

Jul 1 '06 #3
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::ostream_iterator<int>(std::cout, " "));
std::cout << "\n";
}
P.Krumins

Jul 1 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
4229
by: Robert Mazur | last post by:
MySQL 5.0 alpha (binary install) on Solaris 9 -or- RedHat 8.0 mysql-connector-java-3.0.8-stable ----------------------- Is there something different going on with JDBC and the alpha version...
1
1841
by: Ken | last post by:
Any thoughts on making explicit calls to unexpected()? My sense is that this function is really intended to be invoked automatically as part of exception handling. Are there times when explicit...
3
2843
by: Teddy | last post by:
Hello all According to "Think in C++ Volume2", the code below should run smoothly: #include <iostream> #include <exception> using namespace std; class ex { };
2
2153
by: Gerhard Esterhuizen | last post by:
Hi, I am observing unexpected behaviour, in the form of a corrupted class member access, from a simple C++ program that accesses an attribute declared in a virtual base class via a chain of...
3
7370
by: Eric Anderson Vianet SAO | last post by:
hello all When i tried pg_dump -v -f dump.dmp dtbtransporte I got the error: pg_dump: restoring data for table tbdmovimento pg_dump: dumping out the contents of table tbdmovimento ...
5
24724
by: Vijayakrishna Pondala | last post by:
Hi, We are using the following error randomly, when accessing a webservice method/servlet hosted on JBoss application server: The underlying connection was closed: An unexpected error occurred...
5
4472
by: r.nikhilk | last post by:
Hi, Currently, we are porting C++ applications from 32 bit to 64 bit on AIX platform. (The current version of AIX is 5.3 and xlC verison is 8.0). We are able to compile the applications by...
8
2808
by: Jim Michaels | last post by:
C:\prj\quiz\withusers>php tareports.php PHP Parse error: syntax error, unexpected T_ELSE in C:\prj\quiz\withusers\tareports.php on line 205 this is the section of code. if (isset($row4)) {...
3
3289
by: Anup Daware | last post by:
Hi Group, I am facing a strange problem here: I am trying to read xml response from a servlet using XmlTextWriter. I am able to read the read half of the xml and suddenly an exception:...
13
3573
by: bintom | last post by:
I ran the following simple code in C++ and got unexpected results: float f = 139.4; cout << f; Output: 139.399994;
0
7188
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7258
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6970
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7441
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5558
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...
1
4987
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4663
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
1489
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.