Hello all,

First of all i am a beginner in vc++, so excuse my question if its simple.

I was wondering if I could use the data from button1 in button 2

my code is:

public:System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{   
		


OpenFileDialog open;
open.Title = "Select Wave File";
open.Multiselect = true;
open.CheckFileExists = true;
open.Filter = "Audio Files (*.wav)|*.wav";
open.FilterIndex = 2;
open.InitialDirectory="C:";
				

if (open.ShowDialog()==System::Windows::Forms::DialogResult::OK )
{
//convert open.FileName to unmanaged memory
String^ managedString =open.FileName;
char* filename = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();
//end convertion

					
ifstream waveFile (filename);//open wav file
			
waveFile.seekg (0, ios::end); // Move the get pointer to the end of the file

					
int fileSize = waveFile.tellg(); // Get the offset of the get pointer
					
waveFile.seekg(ios::beg);//get pointer back to the beginning
				
unsigned char *fileData = new unsigned char [fileSize];// Allocate memory for the data
			        
waveFile.read ((char *)fileData, fileSize);// Read the data into memory
int offset = 12;
WaveFmtSubChunk *fmtChunk = (WaveFmtSubChunk *)(fileData + offset);

// Loop through sub chunks.  The fmt may not be the first one.
while (strncmp (fmtChunk->id, "fmt ", 4))
{
offset++;
if (offset > fileSize - 4)
{

delete [] fileData;
						    
}
fmtChunk = (WaveFmtSubChunk *)(fileData + offset);
}
if (fmtChunk->format != 1)
{
						 
delete [] fileData;
						
}
offset += 8; // (8 for fmt id and size members)
WaveDataSubChunk *dataChunk = (WaveDataSubChunk *)(fileData + fmtChunk->size + offset);
for (int i=0;i<10;i++)
if (!strncmp (dataChunk->id, "data", 4))
break;
else
dataChunk = (WaveDataSubChunk *)(((char *)dataChunk) + dataChunk->size + 8);
if (strncmp (dataChunk->id, "data", 4))
{
						
delete [] fileData;
					    
}
					
textBox1->Text=open.FileName;

}
}

 public:System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 

{
		   
SaveFileDialog save;
save.Title = "Save Report";
save.CheckFileExists = false;
save.Filter = "txt Files (*.txt)|*.txt";
save.FilterIndex = 2;
save.InitialDirectory="C:";
if (save.ShowDialog()==System::Windows::Forms::DialogResult::OK )
{
   //convert open.FileName to unmanaged memory				
String^ managedString =save.FileName;				 
char* savefile = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();			         
//end convertion

std::ofstream writeReport(savefile); //generating the report file
if (writeReport.is_open() )  //check if file was created
{
//write the necessary data in the file
writeReport<<"File Size"<<fileSize<<" bytes"<<"\n"<<"Audio Sample Size"<<fmtChunk->size<<"bit"<<"\n"<<"Channels Nr."<<fmtChunk->channels<<"\n"<<"Audio SampleRate"<<fmtChunk->sampleRate<<" Hz"; //her is the error
}
else 
{
exit(0); //if the report file is not created the program exits
}

textBox2->Text=save.FileName;
}
}

I get the following errors

c:\documents and settings\uidl7181\desktop\audio analyzer\audio analyzer\Form1.h(296) : error C2065: 'fileSize' : undeclared identifier
1>c:\documents and settings\uidl7181\desktop\audio analyzer\audio analyzer\Form1.h(296) : error C2065: 'fmtChunk' : undeclared identifier
1>c:\documents and settings\uidl7181\desktop\audio analyzer\audio analyzer\Form1.h(296) : error C2227: left of '->size' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\documents and settings\uidl7181\desktop\audio analyzer\audio analyzer\Form1.h(296) : error C2227: left of '->channels' must point to class/struct/union/generic type
1> type is ''unknown-type''
1>c:\documents and settings\uidl7181\desktop\audio analyzer\audio analyzer\Form1.h(296) : error C2227: left of '->sampleRate' must point to class/struct/union/generic type
1> type is ''unknown-type''

someone??? please

Those are pretty clear compiler errors.
I'd suggest you try to resolve them, and ask specific questions about them if you can't figure out how to go about that.

Those errors are not very clear for me.
i will post the code that works but i do not need that.

I need to read the wave file in button1 and then generate a report file by pressing button2.

public:System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) {      OpenFileDialog open;open.Title = 

"Select Wave File";open.Multiselect = true;open.CheckFileExists = true;open.Filter = "Audio Files 

(*.wav)|*.wav";open.FilterIndex = 2;open.InitialDirectory="C:";  if 

(open.ShowDialog()==System::Windows::Forms::DialogResult::OK ){//convert open.FileName to unmanaged memoryString^ 

managedString =open.FileName;char* filename = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();//end 

convertion  ifstream waveFile (filename);//open wav file waveFile.seekg (0, ios::end); // Move the get pointer to the end of 

the file  int fileSize = waveFile.tellg(); // Get the offset of the get pointer waveFile.seekg(ios::beg);//get pointer back 

to the beginning unsigned char *fileData = new unsigned char [fileSize];// Allocate memory for the data waveFile.read ((char 

*)fileData, fileSize);// Read the data into memoryint offset = 12;WaveFmtSubChunk *fmtChunk = (WaveFmtSubChunk *)(fileData + 

offset); // Loop through sub chunks.  The fmt may not be the first one.while (strncmp (fmtChunk->id, "fmt ", 4)){offset++;if 

(offset > fileSize - 4){ delete [] fileData; }fmtChunk = (WaveFmtSubChunk *)(fileData + offset);}if (fmtChunk->format != 1){ 

delete [] fileData; }offset += 8; // (8 for fmt id and size members)WaveDataSubChunk *dataChunk = (WaveDataSubChunk 

*)(fileData + fmtChunk->size + offset);for (int i=0;i<10;i++)if (!strncmp (dataChunk->id, "data", 4))break;elsedataChunk = 

(WaveDataSubChunk *)(((char *)dataChunk) + dataChunk->size + 8);if (strncmp (dataChunk->id, "data", 4)){ delete [] fileData; 

} textBox1->Text=open.FileName; }}  public:System::Void button2_Click(System::Object^  sender, System::EventArgs^  e)  { 

SaveFileDialog save;save.Title = "Save Report";save.CheckFileExists = false;save.Filter = "txt Files 

(*.txt)|*.txt";save.FilterIndex = 2;save.InitialDirectory="C:";if 

(save.ShowDialog()==System::Windows::Forms::DialogResult::OK ){   //convert open.FileName to unmanaged memory			

	String^ managedString =save.FileName;				 char* savefile = (char*) 

Marshal::StringToHGlobalAnsi(managedString ).ToPointer();			         //end convertion std::ofstream 

writeReport(savefile); //generating the report fileif (writeReport.is_open() )  //check if file was created{//write the 

necessary data in the filewriteReport<<"File Size"<<fileSize<<" bytes"<<"\n"<<"Audio Sample 

Size"<<fmtChunk->size<<"bit"<<"\n"<<"Channels Nr."<<fmtChunk->channels<<"\n"<<"Audio SampleRate"<<fmtChunk->sampleRate<<" 

Hz"; //her is the error}else {exit(0); //if the report file is not created the program exits} 

textBox2->Text=save.FileName;}}public:System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{   
		


OpenFileDialog open;
open.Title = "Select Wave File";
open.Multiselect = true;
open.CheckFileExists = true;
open.Filter = "Audio Files (*.wav)|*.wav";
open.FilterIndex = 2;
open.InitialDirectory="C:";
				

if (open.ShowDialog()==System::Windows::Forms::DialogResult::OK )
{
//convert open.FileName to unmanaged memory
String^ managedString =open.FileName;
char* filename = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();
//end convertion

					
ifstream waveFile (filename);//open wav file
			
waveFile.seekg (0, ios::end); // Move the get pointer to the end of the file

					
int fileSize = waveFile.tellg(); // Get the offset of the get pointer
					
waveFile.seekg(ios::beg);//get pointer back to the beginning
				
unsigned char *fileData = new unsigned char [fileSize];// Allocate memory for the data
			        
waveFile.read ((char *)fileData, fileSize);// Read the data into memory
int offset = 12;
WaveFmtSubChunk *fmtChunk = (WaveFmtSubChunk *)(fileData + offset);

// Loop through sub chunks.  The fmt may not be the first one.
while (strncmp (fmtChunk->id, "fmt ", 4))
{
offset++;
if (offset > fileSize - 4)
{

delete [] fileData;
						    
}
fmtChunk = (WaveFmtSubChunk *)(fileData + offset);
}
if (fmtChunk->format != 1)
{
						 
delete [] fileData;
						
}
offset += 8; // (8 for fmt id and size members)
WaveDataSubChunk *dataChunk = (WaveDataSubChunk *)(fileData + fmtChunk->size + offset);
for (int i=0;i<10;i++)
if (!strncmp (dataChunk->id, "data", 4))
break;
else
dataChunk = (WaveDataSubChunk *)(((char *)dataChunk) + dataChunk->size + 8);
if (strncmp (dataChunk->id, "data", 4))
{
						
delete [] fileData;
					    
}
std::ofstream writeReport("report.txt"); //generating the report file
if (writeReport.is_open() )  //check if file was created
{
//write the necessary data in the file
writeReport<<"File Size"<<fileSize<<" bytes"<<"\n"<<"Audio Sample Size"<<fmtChunk->size<<"bit"<<"\n"<<"Channels 

Nr."<<fmtChunk->channels<<"\n"<<"Audio SampleRate"<<fmtChunk->sampleRate<<" Hz"; //her is the error
}
else 
{
exit(0); //if the report file is not created the program exits
}
					
textBox1->Text=open.FileName;

}
}

 public:System::Void button2_Click(System::Object^  sender, System::EventArgs^  e) 

{
		   
SaveFileDialog save;
save.Title = "Save Report";
save.CheckFileExists = false;
save.Filter = "txt Files (*.txt)|*.txt";
save.FilterIndex = 2;
save.InitialDirectory="C:";
if (save.ShowDialog()==System::Windows::Forms::DialogResult::OK )
{
   //convert open.FileName to unmanaged memory				
//String^ managedString =save.FileName;				 
//char* savefile = (char*) Marshal::StringToHGlobalAnsi(managedString ).ToPointer();			         
//end convertion



textBox2->Text=save.FileName;
}
}

Those are pretty clear compiler errors.
I'd suggest you try to resolve them, and ask specific questions about them if you can't figure out how to go about that.

I agree....

@OP:
I suggest you re-evaluate your formatting and work on readability. It appears that you are not matching up a pair of open and close braces correctly ( '{' and '}') but I can't tell for sure. If you look at your errors, there are numbers in parentheses, these numbers are line numbers. I would suggest you look at line 296 in your source file, then trace backward until you find the scoping issue that you seem to have.

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.