0

I want to write a program to read a audio file and classify the various symbols into the number of times they occur , I was hoping i could use a switch statement and classify the 256 symblos but now i've learnt the standard ascii set is only from 0-127,so the rest of the symbols would vary from system to system?.

So how do i go on reading these symbols such that it would work on any system and also how can i access thier decimal values ??

I'm just hoping somebody points me in the right direction,Thankyou.

3
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by Ancient Dragon
0

Open the file in binary mode, not text, then read each byte into an unsigned char, which has a value 0-255. If you want to count the number of occurrences all you need is an array of 255 and use the char as the index into the array

int counters[255] = {0}

ifstream in("myfile.bin", ios::binary);
unsigned char c;
while( in.read((char *)&c, 1) )
{
   counters[c]++;
}
0

Open the file in binary mode, not text, then read each byte into an unsigned char, which has a value 0-255. If you want to count the number of occurrences all you need is an array of 255 and use the char as the index into the array

int counters[255] = {0}

ifstream in("myfile.bin", ios::binary);
unsigned char c;
while( in.read((char *)&c, 1) )
{
   counters[c]++;
}

Thanks a lot,but i'm new to this and don't understand this part

while( in.read((char *)&c, 1) )

Why the use of a pointer and why reference &c ?

0

The first argument in.read requires is a pointer to a signed character buffer. The second is the length of that buffer.
char c;
But that is a signed buffer, which takes values in the range of -128...0...127 but you needed unsigned 0...255 so an unsigned char was used.
unsigned char c;

That is a character buffer with the length of one character.
To get the pointer one had to dereference the buffer.
&c
Which is the address of <c> which is what the function required.
Because the function needed a signed buffer it was then cast to
(char *)&c
Resulting int the
in.read( (char*) &c, 1 )

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.