C/C++ Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 
Go Back   Dev Articles Community ForumsProgrammingC/C++ Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old April 16th, 2005, 07:10 PM
Parkin_m Parkin_m is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 10 Parkin_m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 58 sec
Reputation Power: 0
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



Reply With Quote
  #2  
Old April 16th, 2005, 07:26 PM
astralvoid astralvoid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 9 astralvoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 42 sec
Reputation Power: 0
Smile

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!

Reply With Quote
  #3  
Old April 16th, 2005, 08:55 PM
Parkin_m Parkin_m is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 10 Parkin_m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 58 sec
Reputation Power: 0
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:
//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(charfileName)
{
 
//Constructs an object of class.
//Consider stream as binary rather than text.
ifstream infile (fileNameofstream::binary);
//Check if stream is good for i/o operations, using a constant. If not good return false?? **how does this work**??
if(infile.good() != 1)
{
cout << "There is a problem with opening "<< fileName
return 
false;
}
 
//really dont understand this bit!!
infile.read((char *)&wstruct,sizeof(wstruct));
 
length wstruct.Subchunk2Size/2;
char pData = new char[wstruct.Subchunk2Size];
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++)
{
infile.read((char *)&convertion_union.b.b0,1);
infile.read((char *)&convertion_union.b.b1,1);
sample[i] = convertion_union.s;
}
 
delete pData;
infile.close();
return 
true;


Reply With Quote
  #4  
Old April 17th, 2005, 07:27 AM
astralvoid astralvoid is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jul 2004
Posts: 9 astralvoid User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 2 m 42 sec
Reputation Power: 0
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;
 }  
  


Reply With Quote
  #5  
Old April 17th, 2005, 07:36 AM
Parkin_m Parkin_m is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2004
Posts: 10 Parkin_m User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 44 m 58 sec
Reputation Power: 0
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

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingC/C++ Help > Help with some c++ needed quickly!!


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway
Stay green...Green IT