title: Counting the frequency of hex characters in a .txt file
body: <p>I have a file that contains hexadecimal values separated by spaces. I want to find the frequency of each of the hex values and write it to a text file. For example, consider the string <code>"1b 17 3c 45 3f 52 7a 5a 3b 45 31 52 2e 17 3e 58 3f 44 "</code>. I want to count the frequency of occurrence of each hex value:</p>
  
  <pre><code>1b - 1 times 
  17 - 2 times
  .... so on.
  </code></pre>
  
  <p>I have currently written a c++ program but it counts the space between the hex characters as a hex character as well and does not function as expected.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iomanip&gt;
  
  using namespace std;
  
  int main() 
  {
      int x  = 0;
      int total[128] = {0};
      int index;
  
      ifstream infile;
      ofstream outfile;
  
      infile.open("hex.txt");
      outfile.open("results2.txt");
      if(!infile)
      {
          cout &lt;&lt; "Error opening input file" &lt;&lt; endl;
          return 0;
      }   
  
      char c;
      while(infile.get(c))
      {
          index = c;
          total[index]++;
      } 
  
      for (int i=0; i&lt;128; i++)      // Print the results
      {
          outfile &lt;&lt; "  " &lt;&lt; hex &lt;&lt; i &lt;&lt; " occurs "
                  &lt;&lt; setw(5) &lt;&lt; dec &lt;&lt; total[i] &lt;&lt; " times" 
                  &lt;&lt; "  " &lt;&lt; endl;
      }
      return 0;
  }
  </code></pre>
  
  <p>Note:</p>
  
  <pre><code>"hex.txt" is the input file
  "results2.txt" is the output file
  </code></pre>