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.

Recommended Answers

All 4 Replies

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]++;
}

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 ?

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 )

And the while loop stops reading on end-of-file.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.