Hi all,

the while loop in my code dont seem to be working as i wanted it to.
I wanted it to constantly check if new data is available in the sample.txt but curently after the button click it just finished with one try.

is it i put everything into the button_click event so it cant do the while loop properly or it is my code had been wriiten wrongly?

private: System::Void button1_Click(System::Object^  sender, System::EventArgs^  e) 
{
	
	
	ifstream in("sample.txt");
	int proCheck = 0;
				if (in==NULL)
				{
					MessageBox::Show ("sample.txt not found");
					/*in.close();*/
					proCheck++;
				}
	while ((in!=NULL)&&(proCheck==0))
	{		
		char checkEmpty;
		in.get(checkEmpty);
		if (!in.eof()) 
		{
			string line;
			std::string match("sample1\.   \:");
			int clock = 0;
			while ((getline(in, line))&&(clock == 0))
			{			
						
				stringstream sstrm(line);
				std::string::iterator iter =
				std::search (
				line.begin(),
				line.end(),
				match.begin(),
				match.end()
				);
												
				if (iter != line.end() )
				{
				 

				 //iterators function to search line by line till word are found
				 //and get the word after it.						
					string::size_type iter = line.find( match );  
						if (iter == string::npos)    
						{    
							MessageBox::Show ("Words not found");
							return;
						}  
						iter = line.find_first_not_of( " \t", iter +match.length() );
						if (iter == string::npos)    {  
							MessageBox::Show ("No number after words");
							return;
						}  
						string::size_type j = line.find_first_of( " \t", iter );
						string found;
						found = line.substr( iter, (j == string::npos) ? j : (j -iter) );

						//create a new file based on the word after said word
						//and saved it into the said folder
						string filename = found + ".txt";
						string dirname = "log//";
						string finaldir = dirname + filename;
						ofstream(finaldir.c_str());

						//using ifstream and ofstream to copy content of file to another
						char * fbuffer;
						long fsize;
						ifstream cpy ("sample.txt",ios::binary);
						ofstream target (finaldir.c_str(),ios::binary);
						  cpy.seekg(0,ifstream::end);
						  fsize=cpy.tellg();
						  cpy.seekg(0);
						  fbuffer = new char [fsize];
						  cpy.read (fbuffer,fsize);
						  target.write (fbuffer,fsize);
						  delete[] fbuffer;
						  target.close();
						  
						//write the found word/serial no. into temp.txt
							const char * buffer=found.c_str();
							 string str (found.c_str());
								 int sizeNum = str.length();
								long size = sizeNum;
								ofstream outfile ("temp.txt");
								outfile.write (buffer,size);
								//delete[] buffer;//release dynamically allocated memory
								outfile.close ();
								//textbox update and check if previous saved name is the same as 
								//current one
								textBox2->Text = System::IO::File::ReadAllText("temp.txt");
										if (textBox2->Text == label2->Text)
										{
											MessageBox::Show ("sample no. existed");
										}
										else
										{
											label2->Text = textBox2->Text;
										}
								/*String^ sstring = textBox2->Text;
								MessageBox::Show (sstring);*/
						//try to use an empty file to write and empty a file 
						//which is being used by another program
						char * ebuffer;
						long esize;
						ifstream ecpy ("empty.txt",ios::binary);
						ofstream etarget ("sample.txt",ios::binary);
						  ecpy.seekg(0,ifstream::end);
						  esize=ecpy.tellg();
						  ecpy.seekg(0);
						  ebuffer = new char [esize];
						  ecpy.read (ebuffer,esize);
						  etarget.write (ebuffer,esize);
						  delete[] ebuffer;
						  etarget.close();
						
						clock++;
						
				} //if iter loop
				textBox1->Text = "Finished copying";
				
			} //while loop
		
		} //if file empty	
		else 
			{
			textBox1->Text = "Awaiting for new data...";
			
			}		 
				
	}//main while loop
				
			 
}

Here is how to monitor file changes in MS-Windows operating systems. You can't do it the way you are trying to.

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.