I am trying to use stream to open a .txt file and pass the data inside it into an array. However, I have the problem that when I pass the file (see below) the compiler understand the digits of each pair as independents (i.e. instead of understanding "ce" as 0xce, it understands "c" as a digit and then "e" as another digit). Is there anything I can do for my program to understand that I am passing a full value rather than just one value at a time as it goes through the file ? Thanks.
std::ifstream myFile( argv[1] ) ;
// https://en.cppreference.com/w/cpp/io/manip/hex
myFile >> std::hex ; // set the numeric base for integer input to 16
unsignedint x ;
while( myFile >> x ) { /* insert x in array */ }
Your use of eof() is wrong as well.
eof() is the result of a previous input, not a prediction for a future input.
If you had 10 chars in a file, and read exactly 10 chars, then eof() would still be false.
This what I have been trying to do in regards to storing the instructions. I tried to perform a bitwise left shift by 4 but it is worse now. The compiler outputs:
Hi thank you for your answer but is there any chance it could be done without having to create a vector ? I have never used vectors before. Also, I am using this code in a class function and I am trying to simplify matters as much as possible since this function is only meant to read the file and store its values into an array.
I would do it as a vector and then extract the array from it if you NEED it in array form.
you can take &vec[0] as a pointer-array and vec.size() for its size (for 0... < vec.size() is all the elements), and from there just treat it as you would a C array.