| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Help with some c++ needed quickly!!
Hi having a problem with some c++
I hope someone can help me becuase i need to get this sorted in my head quickly!Basically I have a function, inside a public class that opens a wav file. this is the code: Code:
Public:bool openWavfile(char* fileName) { //**does some stuff here**// }}; then i have a main function which calls the function (and sends music.wav) Code:
(void) main (void)
w.openWavfile("C:\\music.wav");
I am confused when i get to the line "bool openWavfile(char* fileName)" From what i understand: this is a function called openWavefile, it returns bool type data. It has an argument (char*fileName) and the argument is a pointer. what i would like to know is: how does the pointer work? i guess that it doesnt give char fileName the value of c:\\music.wav. Does it assign the the address in memory of C:\\music.wav or is it just a decleration of a pointer? Im really stuck on this, its baffling me! if you could either post here, or add me to MSN Parkin_m@hotmail.com if you dont mind having a quick chat with me... PLease be quick! Thanks Michael |
|
#2
|
|||
|
|||
|
You are right about the argument being a pointer, but in this case, don't let it confuse you too much.
You are effectively passing a reference to the string you are sending to the function. In C, strings are basically arrays of characters and the name of a character array can be used as the reference since it 'points to' the location in memory where the string is located; in your example, "c:\\music.wav" the address of this string is passed to the function. Just rememebr that the name of any array in C is actually a reference to its location in memory. I hope this helps and I didn't confuse you more! |
|
#3
|
|||
|
|||
|
Im trying to understand this fully, would it be possible for you to write comments all over it to say what is happening? i Have given it my best attempt! THanks very much btw for your help, it really is appreciated!
PHP Code:
|
|
#4
|
|||
|
|||
|
Here is an attempt..
My suggestion to help you out on this and future items would be to study pointers, classes, unions and structures in C++. There is an enormous collection of help material by starting with google.com. Even though I've commented the code, most of the items would be understood once you understand the topics i listed above. Hope it helps. Code:
//open wavfile function. Declare char pointer (filename). The address of this string is passed to the function since it 'points to' the location in memory where the string is located
bool openWavfile(char* fileName)
{
//Constructs an object of class.
//Consider stream as binary rather than text.
ifstream infile (fileName, ofstream::binary);
//Check if stream is good for i/o operations, using a constant. If not good return false?? **how does this work**??
// .good() is a method of infile object that returns 1 if true; otherwise false. So, by checking if infile.good() method returns a 1, you can continue, otherwise display error message and return false from the current function, openWavFile().
if(infile.good() != 1)
{
cout << "There is a problem with opening "<< fileName;
return false;
}
//really dont understand this bit!!
//.read() is a method of the infile object that asks you to pass it a filename(pointer to char - just like the calling function),
//the address of a struct to hold the record it will read from the file,
// and the size in bytes of that struct so it will know how many bytes to read for the struct.
//I can only assume(since it isn't shown in your code) that the struct is used for a header of the wav file you're wanting to read
infile.read((char *)&wstruct,sizeof(wstruct));
//defines length to be some value from the wstruct you just filled in. SubchunkSize is a member of the wstruct
length = wstruct.Subchunk2Size/2;
//create a pointer to a new char array of size wstruct.SubchunkSize
char * pData = new
char[wstruct.Subchunk2Size];
//define sample = pointer to new short array of size length
sample = new short[length];
int charCount = 0;
union uni
{
short s;
struct bytes
{
char b0;
char b1;
}b;
}convertion_union;
for(int i =0;i<length;i++)
{
//read each record/item from the file into the convertion_union struct you created above
infile.read((char *)&convertion_union.b.b0,1);
infile.read((char *)&convertion_union.b.b1,1);
sample[i] = convertion_union.s;
}
//deletes the pointer from memory and frees the memory back to the o/s
delete pData;
//closes the infile object
infile.close();
//since we got this far, then everything must have worked, so we can return true.
return true;
}
|
|
#5
|
|||
|
|||
|
thank you very very much! I will continue to finish the rest of the code off and comment it. This will really help me with my learning, and i will be able to understand it next time
thanks again, Michael |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Help with some c++ needed quickly!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|