How to write from array to outputfile?
I have a vector vec (256, 0) that I use to record type of characters and
their frequency from the input file. So given 3 A's my vec[65] will hold
value of 3. I am trying to write to output file the count non-empty chars
in total followed by their ascii character and the frequency of
occurrence.
int count = 0;
for (int i = 0; i < 256; i++)
if (vec[i] != 0) // if not 0 count it
count++;
// print the count as the first char in output file
outfile << count;
for (int i = 0; i < 256; i++)
if (vec[i] != 0)
outfile << (char) i << vec[i];
Given input "a bb c" what I want is:
4a1b2c1
But what I get is:
5
1 2a1b2c1
What can I do to fix this?
No comments:
Post a Comment