Page 1 of 1

Read text from Text File

Posted: Sun Jun 27, 2010 11:11 am
by Nerrazzuri
Need some help on my NRZBot v2.1

I'm able to copy the texts on every textbox to a .txt file using StreamWriter class but hell I can't retrieve it back to the textbox from the .txt file.

here is the code i use to write to the .txt file

Code: Select all

String^Skill1 = textBox2->Text;
String^Skill2 = textBox3->Text;
String^Skill3 = textBox4->Text;
String^Charge1 = textBox5->Text;
String^Charge2 = textBox6->Text;
String^Charge3 = textBox7->Text;  
String^ValueHP = textBox1->Text;
String^ValueMP = textBox8->Text;
StreamWriter^ sWriter = gcnew StreamWriter("c:\\BotSettings.txt");
sWriter->WriteLine(Skill1);
sWriter->WriteLine(Skill2);
sWriter->WriteLine(Skill3);
sWriter->WriteLine(Charge1);
sWriter->WriteLine(Charge2);
sWriter->WriteLine(Charge3);
sWriter->WriteLine(ValueHP); 
sWriter->WriteLine(ValueMP);
sWriter->Close();
And I've tried to read through some examples on google but return no result. Wish someone who can help me on this. :(

Re: Read text from Text File

Posted: Sun Jun 27, 2010 2:03 pm
by zile
I'm not using MFC, so im not sure if this can be used.

get the line of the file with ifstream.h into a variable
then SetDlgItemText for that textbox,
then continue getting the next line

http://www.cplusplus.com/doc/tutorial/files/
hope it helps

Re: Read text from Text File

Posted: Sun Jun 27, 2010 2:21 pm
by Nerrazzuri
zile wrote:I'm not using MFC, so im not sure if this can be used.

get the line of the file with ifstream.h into a variable
then SetDlgItemText for that textbox,
then continue getting the next line

http://www.cplusplus.com/doc/tutorial/files/
hope it helps
I'm not using MFC either, I'm using Windows Form C++/CLI class, so SetDlgItemText doesn't work for me. :cry:

Re: Read text from Text File

Posted: Sun Jun 27, 2010 2:31 pm
by NoobHacker
Nerrazzuri wrote:Need some help on my NRZBot v2.1

I'm able to copy the texts on every textbox to a .txt file using StreamWriter class but hell I can't retrieve it back to the textbox from the .txt file.

here is the code i use to write to the .txt file

Code: Select all

String^Skill1 = textBox2->Text;
String^Skill2 = textBox3->Text;
String^Skill3 = textBox4->Text;
String^Charge1 = textBox5->Text;
String^Charge2 = textBox6->Text;
String^Charge3 = textBox7->Text;  
String^ValueHP = textBox1->Text;
String^ValueMP = textBox8->Text;
StreamWriter^ sWriter = gcnew StreamWriter("c:\\BotSettings.txt");
sWriter->WriteLine(Skill1);
sWriter->WriteLine(Skill2);
sWriter->WriteLine(Skill3);
sWriter->WriteLine(Charge1);
sWriter->WriteLine(Charge2);
sWriter->WriteLine(Charge3);
sWriter->WriteLine(ValueHP); 
sWriter->WriteLine(ValueMP);
sWriter->Close();
And I've tried to read through some examples on google but return no result. Wish someone who can help me on this. :(
for me i will use array

Code: Select all


string^writedata = textBox2->text +";" + textBox3->text + ";" + textBox4->text + ";" + textBox5->text + ";" + textBox6->Text + ";" + textBox7->Text + ";" + textbox1->text + ";" + textBox8->text;
StreamWriter SR = gcnew StreamWriter("C:\\settings.txt");
SR->Write(writedata);
SR->dispose();
for reading,use this

Code: Select all

Streamreader^SR = gcnew StreamReader("C:\\Settings.txt");
string[]^readdata = SR->Readtoend->Split(";");
textBox2->text = string[0]->tostring();
textBox3->Text = string[1]->ToString();
//blablabla
NOte:you can use \n to spllit the data u wrote using streamwriter

Re: Read text from Text File

Posted: Sun Jun 27, 2010 3:50 pm
by Nerrazzuri
Instead of using your codes, this is mine.

Code: Select all

String^ delimiter = ";";
String^ fileName = "c:\\Settings.txt";
StreamReader^ sr = gcnew StreamReader(fileName);

			try
			{
				while (sr->Peek() >= 0)
				{
					String^ r = sr->ReadLine();
					array<String^>^ items = r->Split(delimiter->ToCharArray());
				}
			}
			finally
			{
				sr->Close();
			}
Ok, got rid of that error. Still it doesn't re-write it back into my textBox. Clueless. >.<

Re: Read text from Text File

Posted: Sun Jun 27, 2010 9:29 pm
by NoobHacker
Nerrazzuri wrote:Instead of using your codes, this is mine.

Code: Select all

String^ delimiter = ";";
String^ fileName = "c:\\Settings.txt";
StreamReader^ sr = gcnew StreamReader(fileName);

			try
			{
				while (sr->Peek() >= 0)
				{
					String^ r = sr->ReadLine();
					array<String^>^ items = r->Split(delimiter->ToCharArray());
				}
			}
			finally
			{
				sr->Close();
			}
Ok, got rid of that error. Still it doesn't re-write it back into my textBox. Clueless. >.<
=/ can show me how you rewrite into your textbox?

Re: Read text from Text File

Posted: Sun Jun 27, 2010 10:22 pm
by Nerrazzuri
NoobHacker wrote:
Nerrazzuri wrote:Instead of using your codes, this is mine.

Code: Select all

String^ delimiter = ";";
String^ fileName = "c:\\Settings.txt";
StreamReader^ sr = gcnew StreamReader(fileName);

			try
			{
				while (sr->Peek() >= 0)
				{
					String^ r = sr->ReadLine();
					array<String^>^ items = r->Split(delimiter->ToCharArray());
				}
			}
			finally
			{
				sr->Close();
			}
Ok, got rid of that error. Still it doesn't re-write it back into my textBox. Clueless. >.<
=/ can show me how you rewrite into your textbox?
I can't, I have no idea how to write it back into the textbox from the text file. :S