0

the problem that I am having is that I should read in the values 7 write 0x01 0x27 from my .txt file and the loop will only run 1 time, giving the output 0 - 7 1 0x01 0x27 (the 1 represents write, 0 = read, 2 = not set), but, the values that I have printed out with the print statement show the values as

0 - 7 1 0x30 0x78
1 - 1 2 0x00 0x00 //where the 2 is it reads in 0x27 as a string

So, I am trying to figure out why it is reading in extra values instead of just the ones in the file.

void ioLoad(char* name)
{   
    std::ifstream inF;
    inF.open(name, ios_base::in);
    int i = 0;
    string rw;
    while(!inF.eof()){
        inF >> var.time[i]; //int time, from struct 'var'
        inF >> rw;          //string rw, should read either read or write
        inF >> std::hex >> var.address[i] //unsigned char address, from struct 'var' >>PROBLEM LINE<<
        if (rw == "write")
        {
            var.state[i] = WRITE; //stores an enum value as the state
            inF >> std::hex >> var.value[i];  //unsigned char value, from struct 'var' >>PROBLEM LINE<< 
        }
        printf("%d - %d %d 0x%02X 0x%02X\n", i, var.time[i], var.state[i], var.address[i], var.value[i]);
        i++
    }
    inF.close()
}

any help would be greatly appreciated and if you need more information please let me know.

amfila
  • 3
  • 2
  • `inF.eof()` returns true if the last read operation hit the end of file, not whether the current read position is at the end of file. – Vaughn Cato Apr 19 '20 at 16:53
  • Please post the actual code in [mcve]. The posted code does not correspond to the expected output format. – 273K Apr 19 '20 at 17:28

2 Answers2

0

using .eof as the condition is a risky buissnes, as the guy commenthing on your post told you, eof runs till the last reading resulted in failure. that means the last action will run twice. here is a good post about this: Why is iostream::eof inside a loop condition (i.e. `while (!stream.eof())`) considered wrong?

there are other options like while(file >> data) or checking where is the end of the file and runing till there.

also note that if you are doing /n between each row of your input file you need to "take it" before you continue, otherwise your next reading action will take it instead of taking the text you want.

  • Thanks, I will take the eof() into consideration and probably change it. but, would that be affecting why it is reading in `0x30 0x78` instead of `0x01 0x27`. – amfila Apr 19 '20 at 17:09
0

var.address[i] and var.value[i] have type unsigned char. Your compiler treats chars as unsigned numerics. So the line inF >> std::hex >> var.address[i] reads chars. You could understand it just looking at the ASCII table.

When inF >> std::hex >> var.address[i] performed, the input is 0x01 and '0' is placed to, that is 0x30. When inF >> std::hex >> var.value[i]; performed, the input is x01 and 'x' is placed to, that is 0x78.

You can read into unsigned int tmp

unsigned tmp;
inF >> std::hex >> tmp;
var.address[i] = static_cast<unsigned char>(tmp);
if (rw == "write") {
  var.state[i] = WRITE;
  inF >> std::hex >> tmp;
  var.value[i] = static_cast<unsigned char>(tmp);
}
273K
  • 29,503
  • 10
  • 41
  • 64