0

I am trying to read two data from a file and I am experiencing two problems:

  1. Infinite loop
  2. The first value is read correctly, the second is not.

I tried playing around with getline but I couldn't get it to work properly. I have included my code in c++, the input file, and the correct output below.

The correct output should be this:

Num1 = 4FD37854
Num2 = E281C40C

There are the two data I'm trying to read from a file called input.txt:

4FD37854
E281C40C

Here is my program:

#include <iostream>
#include <fstream>

using namespace std;

union newfloat{
    float f;
    unsigned int i;
};

int main ()
{

// Declare new floating point numbers
newfloat x1;
newfloat x2;

// Create File Pointer and open file (destructor closes it automatically)
ifstream myfile ("input.txt");

while (myfile >> hex >> x1.i) // Read until at EOF
{

myfile >> hex >> x2.i; // Read input into x2

cout << "Num1 = " << hex << x1.i << endl;
cout << "Num2 = " << hex << x2.i << endl;

} // end of file reading loop
return 0;
}
Veridian
  • 3,278
  • 10
  • 38
  • 73
  • 2
    You should check that the read operations succeed, i.e. `if (!(myfile >> hex >> x1.i)) { /* read failed */ }` – Drew McGowen Jan 13 '15 at 19:06
  • @DrewMcGowen Tried that, didn't show any read failures. I tried it before and within the while loop also. – Veridian Jan 13 '15 at 19:09
  • 2
    I.e fix this regardless: [`while (!myfile.eof()) `](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). – WhozCraig Jan 13 '15 at 19:12
  • @WhozCraig, k, I think I fixed that part. – Veridian Jan 13 '15 at 19:24
  • What are you trying to do here: `((exp2 && exp2) << 23)`? – wimh Jan 13 '15 at 19:24
  • 1
    Try an `unsigned int` in the union. – molbdnilo Jan 13 '15 at 19:26
  • @Wimmel 0 if `exp2` is zero, 1 << 23 if it's non-zero. – molbdnilo Jan 13 '15 at 19:28
  • @Wimmel, that's for the hidden bit of the mantissa. If the exponent isn't zero, include the hidden bit. I check if it's zero by performing a logical or of the exponent with itself. If the result is 1, I shift that bit into the 24th position for the hidden bit (part of floating point format). – Veridian Jan 13 '15 at 19:29
  • @Wimmel, I updated the code to get rid of anything regarding floating point with the hidden bits and such. – Veridian Jan 13 '15 at 19:31
  • @molbdnilo, THAT WORKED! Please post it as a solution so I can accept it. – Veridian Jan 13 '15 at 19:34
  • @starbox I simply was not aware that the conversion from bool to int was explicitly [defined](http://stackoverflow.com/a/5369783/33499) – wimh Jan 13 '15 at 19:34

2 Answers2

3

while (!myfile.eof()) is almost always wrong, and will read one more time than you expect.

You should say

while(myfile >> hex >> x1.i >> x2.i)

But the main issue is that E281C40C can't be read into an int, you need an unsigned int.

This is also the reason for your infinite loop - since the read fails before reaching the end of the file, !myfile.eof() keeps being true, and the reading keeps failing.
Which is one more reason to avoid eof().

molbdnilo
  • 55,783
  • 3
  • 31
  • 71
0

So, lets look at the second problem.

  1. The second value read is for some reason read incorrectly.

Well, that's actually an input problem. You have entered 0xE281C40C, while the maximum value of int is 0x7FFFFFFF. You can simply change the definition of newFloat to:

union newfloat{
    float f;
    unsigned int i;
};

and it will accept values bigger than 0x7FFFFFFF

  1. Infinite loop

I don't know why it happens, and it didn't happen on my machine. It might not happen on your machine after you'll fix the second problem, though.

Curve25519
  • 584
  • 4
  • 17