Trouble passing two digit hex values from .txt file into array

Hi,

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.

main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;

int main(int argc, char* argv[])
{

int i=0;
int myArray[]={};

 ifstream myFile(argv[1]);
  
    while (!myFile.eof()){
    myFile >> myArray[i];
   i++;
    
    }

   myFile.close();
    return 0;
}



My .txt file
1
2
3
4
5
6
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
c9 ce 01 f0 a8 ce 00 05 8b b3 08 8e 04 03 2d 88
3f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00

Last edited on
1
2
3
4
5
6
7
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

unsigned int x ;
while( myFile >> x ) { /* insert x in array */ } 
Last edited on
You would need to read each pair as a string, then use stoi with a base of 16.
http://www.cplusplus.com/reference/string/stoi/

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.

So typically, write something like
1
2
3
while ( myFile >> x ) {
    cout << x << endl;
}
Hi, thank you all for your replies but it is still not working. My output window continues to display (for example):


Memory[0x20] = c;
Memory[0x21] = 9;
Memory[0x22] = c;
Memory[0x23] = e;

When I want it to display it in this format:

Memory[0x20] = c9;
Memory[0x21] = c3;

and so on. Does anyone know how to achieve this ?
for(all the digits)
{
cout << memoryloc << " = " << firstdigit << seconddigit;
memoryloc++;
}

you don't need to convert it into numerical form if you just want to redisplay it.

if you would show your code we could hack on it better...
Last edited on
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:

Memory[0x20]=w;

And the rest is all blank. This is my code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
ifstream myFile(file); 
   if (!myFile){  
       cout << "Error opening the file" << endl;
   }

  // myFile >> hex;



   while( myFile >> Memory[i])
  {
       l=(i+1);
       myFile >> hex >> Memory[i];
       Memory[i]=(Memory[i] << 4);
       Memory[i]+=Memory[l];       
   }
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#include <fstream>
#include <vector>
#include <iomanip>

std::vector<unsigned int> read_hex( std::istream& stm )
{
    std::vector<unsigned int> vec ;

    stm >> std::hex ; // set the numeric base for integer input to 16

    unsigned int number ;
    while( stm >> number ) vec.push_back(number) ;

    return vec ;
}

int main()
{
    const char file_name[] = "hex_bytes.txt" ;

    {
        // create a test file
        std::ofstream(file_name) << "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"
                                    "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"
                                    "c9 ce 01 f0 a8 ce 00 05 8b b3 08 8e 04 03 2d 88\n"
                                    "3f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n" ;
    }

    std::ifstream file(file_name) ; // open file for input
    const auto Memory = read_hex(file) ;

    std::cout << std::hex << std::internal << std::setfill('0');
    for( std::size_t i = 0x20 ; i < Memory.size() ; ++i )
    {
        std::cout << "Memory[" << std::showbase << std::setw(4) << i << "] = "
                  << std::noshowbase << std::setw(2) << Memory[i] << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/b8937d11520e2b96
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.
Last edited on
> is there any chance it could be done without having to create a vector ?
> I am trying to simplify matters as much as possible

Using std::vector would be the simplest.

If a raw array must be used, something like this, perhaps:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <fstream>
#include <iomanip>

template < std::size_t N > // return number of items read
std::size_t read_hex( std::istream& stm, unsigned int (&array)[N] )
{

    stm >> std::hex ; // set the numeric base for integer input to 16

    std::size_t i = 0 ;
    unsigned int number ;
    while( i < N && stm >> number ) array[i++] = number ;

    return i ;
}

int main()
{
    const char file_name[] = "hex_bytes.txt" ;

    {
        // create a test file
        std::ofstream(file_name) << "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"
                                    "00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n"
                                    "c9 ce 01 f0 a8 ce 00 05 8b b3 08 8e 04 03 2d 88\n"
                                    "3f 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00\n" ;
    }

    std::ifstream file(file_name) ; // open file for input

    unsigned int Memory[128] ;
    const std::size_t nread = read_hex( file, Memory ) ;

    std::cout << std::hex << std::internal << std::setfill('0');
    for( std::size_t i = 0x20 ; i < nread ; ++i )
    {
        std::cout << "Memory[" << std::showbase << std::setw(4) << i << "] = "
                  << std::noshowbase << std::setw(2) << Memory[i] << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/369ca2e7f9d768ed
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.
Last edited on
Topic archived. No new replies allowed.