   I/O the C++ way ?
  I am currently working on another tiny small project app which is loading a wave file, mess with it a bit and then saves it to a file...The thing is, I take a little pride in trying to do things in C++ rather than C, like using iterators, function objects, STL and such. Everytime that I try something like loading a wavefile I find it so much easier doing it in C-style (mixed with C++), with functions like strcmp(), memcpy() and the like ...So here's the question.Am I missing something, what would be the 'correct' way, using iterators, to load Bytes into memory, comparing sequences and such ?  - Also, to single out all the answers about C beeing a part of C++, we all know about that. This is a question of programmingstyle.Example of what I do to check for the wave file tags:char *pattern  = "RIFF....WAVEfmt ";char *fpattern = "................";file.read(fpattern, 16);memset(fpattern+4, '.', 4); bool isWave = (strcmp(pattern, fpattern) == 0);EDIT:I know of course about iostream iterators but how would you go about reading and comparing bytes, WORDS, DWORDS and such, in a 'nice' way[edited by - JOL on February 13, 2004 7:56:11 AM]
  Not answering your question but...char *pattern  = "RIFF....WAVEfmt ";this declares a pointer to some static memory. It should really beconst char *pattern  = "RIFF....WAVEfmt ";but c++ lets you get away with it being non const. Anyway, you shouldn''t modify that string. ;  I get you point!...I'll throw in a couple of new and delete...   [edited by - JOL on February 13, 2004 9:21:10 AM];  Hello JOL,You are missing the main point about C/C++ programming language.If the language that gives you tools to use, then use the best tools for the job.C++ has at it core C, therefore it perfectly fine to use C's library of routines.I my opinion the C++ I/O is not well suited for binary editing of data. It is great at streaming data, just not well suited for editing the data once gotten.The point is you are not required to use only features in C++ and sometimes it does not even make sense to use C++ features.I use C++ streams all to time to read and write binary data but if your going to edit it I don't use them, to much of a hassle.In your case the C functions are a fine use in C++. Lord Bart  [edited by - lord bart on February 13, 2004 9:28:25 AM];quote:Original post by JOL    I get you point!...I'll throw in a couple of new and delete...   actually you don't need to use new and delete for temporary variable:char pattern[]  = "RIFF....WAVEfmt ";char fpattern[] = "................";file.read(fpattern, sizeof(fpattern)/sizeof(char));memset(fpattern+4, '.', 4);bool isWave = (strncmp(pattern, fpattern) == 0);   [edited by - petewood on February 13, 2004 9:51:33 AM];  Thanks, great answers... ;  This some of my C++ wave code: namespace MKH{namespace Audio{#ifdef _MSC_VER#pragma pack(push)#pragma pack(1)#elif defined __GNUC__#pragma pack(1)#endifstruct fourcc{fourcc() {}fourcc(char a, char b, char c, char d){cc[0]=a;cc[1]=b;cc[2]=c;cc[3]=d;}fourcc(const char* szfcc){memcpy(cc, szfcc, 4);}char cc[4];char& operator[](int i) {return cc[i];}operator u32&() {return *reinterpret_cast<u32*>(this);}u32& Dword() {return *reinterpret_cast<u32*>(this);}};struct RiffHeader{RiffHeader(u32 length=0) : riff("RIFF"), length(length), filetype("WAVE"){}fourcc riff;u32  length; //filesize-8fourcc filetype;};struct RiffChunkHeader{RiffChunkHeader(fourcc fcc=fourcc("data"), u32 length = 8) : chunk(fcc), length(length-8){}fourcc chunk;u32  length;};static const u16 WAVEFORMAT_PCM   = 0x0001;static const u16 WAVEFORMAT_ULAW  = 0x0101;static const u16 WAVEFORMAT_ALAW  = 0x0102;static const u16 WAVEFORMAT_ADPCM = 0x0103;struct PCMExtention{PCMExtention(u16 bps=16) : bits_per_sample(bps){}u16  bits_per_sample;};struct WaveChunk : RiffChunkHeader{WaveChunk(u16 channels=2, u32 rate_hz=44100) : RiffChunkHeader("fmt ", sizeof(WaveChunk)+sizeof(PCMExtention)), format(WAVEFORMAT_PCM),channels(channels), sample_rate(rate_hz), avg_byte_rate(channels*rate_hz*sizeof(short)), block_align(channels*sizeof(short)){}u16  format;  //enum WAVE_FORMAT_PCM=1, uLaw=0x101, aLaw=0x0102, ADPCM=0x0103u16  channels;u32  sample_rate;u32  avg_byte_rate;u16  block_align;};struct WaveFormat{WaveFormat() : channels(2), bits_per_sample(16), sample_rate(44100){}int channels;int bits_per_sample;int sample_rate;};#ifdef _MSC_VER#pragma pack(pop)#elif defined __GNUC__#pragma pack()#endif}//ns Audio}//ns MKHIt''s a binary file, I wouldn''t use string ops on it. bool WaveFile::ReadWaveHeader(){RiffHeader riffheader;WaveChunk wavechunk;PCMExtention pcmExtention;RiffChunkHeader chunkheader;//cout << "riffheader" << sizeof(riffheader) << endl;if(!this->file.Read(&riffheader))return false;if(riffheader.filetype != fourcc(''W'', ''A'', ''V'', ''E'')&&riffheader.filetype != fourcc(''w'', ''a'', ''v'', ''e''))return false;//...