0

I am trying to load this correct_value file into a struct and then insert into a map. The correct_value file has uint_64 numbers, uint_8 numbers printed as text. See the CorrValueEntry struct that I provided. I narrow down to the problem that I am not getting the correct value from file>>a, I printed out a after each time file>>a is called, it's outputing 0.

Here is the struct for the map

struct CorrValueEntry
{
uint64_t seqnum;
uint64_t actual_val;
uint64_t pc;
uint8_t piece;
uint8_t insn;
uint64_t dst;
uint64_t src1;
uint64_t src2;
uint64_t src3;

public: 
CorrValueEntry(): seqnum(0),actual_val(0),pc(0),insn(8),piece(0),src1(0),src2(0),src3(0),dst(0) {}

};

Here is the code that I try to read the correct_value file

 std::fstream file("correct_value");
 uint64_t a;
   if(file.is_open()) {
    while (file >> a)
        {
         CorrValueEntry temp; 
         uint64_t temp_value = a;

         //load seq_no
         file >> a;
         temp.seqnum = a;

         //load actual value
         file >> a;
         temp.actual_val = a;

         //load pc 
         file >> a;
         temp.pc = a;

         //load piece
         file >> a;
         temp.piece = a;

         //load instruction type
         file >> a;
         temp.insn = a;

         //load dst
         file >> a;
         temp.dst = a;

         //load src1
         file >> a;
         temp.src1 = a;

         //load src2
         file >> a;
         temp.src2 = a;

         //load src3
         file >> a;
         temp.src3 = a;


         CVT.insert(std::pair<uint64_t,CorrValueEntry>(temp_value,temp));
       }
         file.close();
   }
   else printf("file not opened\n"); 

Here is the line to declear the map

std::map<uint64_t,CorrValueEntry> CVT;

Here is a sample of the correct value file, note that the first number is the key, and it equals to seqnum; 0x51b49c or such is printed as 0x%lx when I recorded those values.

0 0 0 0x51b49c 0 0 12 9 11 3735928559
2 2 4814308436412893359 0x51b4a4 0 0 9 9 11 3735928559
4 4 640 0x51b4ac 0 1 9 8 3735928559 3735928559
5 5 65535 0x51b4b0 0 0 10 3735928559 3735928559 3735928559
6 6 66175 0x51b4b4 0 0 9 9 10 3735928559
8 8 40499520 0x51b4bc 0 1 22 22 3735928559 3735928559
10 10 0 0x51b424 0 0 9 22 3735928559 3735928559
Summerle
  • 73
  • 1
  • 5
  • 3
    `while (!file.eof())` https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong – drescherjm Apr 23 '19 at 18:57
  • @drescherjm hi, I tried using file>>a as the condition, still not working. it's getting 0 all the time. – Summerle Apr 23 '19 at 19:03
  • [Sometimes it helps to step back a bit and see where things are failing.](https://ideone.com/hClkkf). Suggestion: Don't write so much code all at once. Write a few lines. Compile. Test. You find bugs a lot faster when they are most likely in the few lines you just added. – user4581301 Apr 23 '19 at 19:12
  • 1
    This doesn't address the question, but your file does **not** have `uint64` or `uint8` **numbers**. It has **text**, and your goal is to **interpret** that text as values of those types. If you slur over that distinction, at some point you'll end up confused. – Pete Becker Apr 23 '19 at 19:15

1 Answers1

0

The hex values are not being accepted correctly.

In order to make it behave correctly, use the unsetf method of file object.

file.unsetf(std::ios::dec);
file.unsetf(std::ios::hex);
file.unsetf(std::ios::oct);

This removes default formatting options.

See also: this answer

h1r0
  • 49
  • 1
  • 5