C++ File IO

Lacking experience in this area...

Page 1 of 1

2 Replies - 599 Views - Last Post: 04 March 2010 - 02:23 PM Rate Topic: -----

Topic Sponsor:

#1 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

C++ File IO

Posted 04 March 2010 - 01:26 PM

Hi guys, I have a program to read the RIFF header (resource interchange file format) of a .wav file, and it messes up at reading into a short variable (2 bytes on my system)...

I'm just making this for entertainment and educational purposes, I could end up doing something with it, but can anyone tell me why this isn't working?? It won't read correctly into the short datatype with the >> or it just fails for some other reason?
My output is only having the first 4 bytes set to "RIFF", after that is fail. You can get a good file to test this prog. with from a wikipedia page, or you can just look at the code, please see the function definition of ReadRiffHeader();
Oh, and can we do aggregate read operations on structures filled with only variables ??

--Edit--
Please take a look at:
https://ccrma.stanfo...cts/WaveFormat/

--Edit2--
Okay, so the >> probably doesn't work too great with binary file io??
I guess I thought it would read the sizeof one short, can anyone shed some light into this?

#define DEBUG
#include <iostream>
#include <fstream>

using namespace std;


struct RIFF_CHUNK_DESCRIPTOR
{
	char RIFF[4]; // spells out 'R I F F'
	int ChunkSize; //36 + SubChunk2Size
	char WAVE[4]; // spells 'W A V E'
};

struct FORMAT_SUB_CHUNK
{
	int SubChunk1Id;
	int SubChunk1Size;
	short AudioFormat; // 2 bytes, should equal 1 (PCM)
	short NumChannels; // 2 bytes
	int SampleRate;
	int ByteRate;
	short BlockAlign;
	short BitsPerSample;
};

struct DATA_SUB_CHUNK
{
	int SubChunk2Id;
	int SubChunk2Size;
};

/* RIFF WAVE PCM */
struct WAVE
{
	RIFF_CHUNK_DESCRIPTOR riffChunk;

	FORMAT_SUB_CHUNK formatSubChunk;

	DATA_SUB_CHUNK dataSubChunk;

	//the data follows all of these
};

/* function prototypes */
int ReadRiffHeader(WAVE &,ifstream &);



int main()
{
	WAVE soundFile;
	memset(&soundFile,0,sizeof(WAVE));
	ifstream inFile("file.wav",ios::in | ios::binary);

	//populate the struct members, and display the SubChunk2Size-36
	ReadRiffHeader(soundFile,inFile);

	inFile.close();


	return 0;
}


int ReadRiffHeader(WAVE & soundFile,ifstream& inFile)
{
	/* populate the riffChunk structure */
	inFile.read(soundFile.riffChunk.RIFF,4);
	inFile >> soundFile.riffChunk.ChunkSize;
	inFile.read(soundFile.riffChunk.WAVE,4);

	/* output info about the riffChunk struct */
#ifdef DEBUG
	cout << "Should spell RIFF: ";
	for(int i=0;i<4;i++)
		cout << soundFile.riffChunk.RIFF[i];
	cout << endl;
	cout << "Chunk Size: " << soundFile.riffChunk.ChunkSize << endl;

	cout << "Should spell WAVE: ";
	for(int i=0;i<4;i++)
		cout << soundFile.riffChunk.WAVE[i];
	cout << endl;
#endif

	/* populate the format sub chunk structure */
	inFile >> soundFile.formatSubChunk.SubChunk1Id;
	inFile >> soundFile.formatSubChunk.SubChunk1Size;
	inFile >> soundFile.formatSubChunk.AudioFormat; // 2 bytes
	inFile >> soundFile.formatSubChunk.NumChannels; // 2 bytes
	inFile >> soundFile.formatSubChunk.SampleRate;
	inFile >> soundFile.formatSubChunk.ByteRate;
	inFile >> soundFile.formatSubChunk.BlockAlign; // 2 bytes
	inFile >> soundFile.formatSubChunk.BitsPerSample; // 2 bytes

	/* output info about the formatSubChunk struct */
#ifdef DEBUG
	cout << "SubChunk1Id: " << soundFile.formatSubChunk.SubChunk1Id << endl;
	cout << "SubChunk1Size: " << soundFile.formatSubChunk.SubChunk1Size << endl;
	cout << "Audio Format (should equal 1 for PCM): " << soundFile.formatSubChunk.AudioFormat << endl;
#endif

	return 0;
}


This post has been edited by taylorc8: 04 March 2010 - 02:47 PM


Is This A Good Question/Topic? 0
  • +

Replies To: C++ File IO

#2 taylorc8  Icon User is offline

  • B&

Reputation: 149
  • View blog
  • Posts: 1,572
  • Joined: 21-July 09

Re: C++ File IO

Posted 04 March 2010 - 02:18 PM

Okay, I think I've solved it, but will this do anything bad?
inFile.read((char*)&soundFile.riffChunk.ChunkSize,sizeof(soundFile.riffChunk.ChunkSize));


Was This Post Helpful? 0
  • +
  • -

#3 Munawwar  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 159
  • View blog
  • Posts: 454
  • Joined: 20-January 10

Re: C++ File IO

Posted 04 March 2010 - 02:23 PM

That is how I would do it.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1