Read text from Text File

Discuss about Programming here. You may release your source, post guide and even ask questions!

Moderator: wizme

Post Reply
User avatar
Nerrazzuri
Destiny General
Destiny General
Posts: 1110
Joined: Sun Dec 20, 2009 9:15 pm

Read text from Text File

Post 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. :(
Selling my ultimate trainer for MapleStory SEA --> View below for screenshot

Click Here for more Information!

Image
zile
Apprantice Knight
Apprantice Knight
Posts: 17
Joined: Sun May 30, 2010 4:46 pm

Re: Read text from Text File

Post 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
User avatar
Nerrazzuri
Destiny General
Destiny General
Posts: 1110
Joined: Sun Dec 20, 2009 9:15 pm

Re: Read text from Text File

Post 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:
Selling my ultimate trainer for MapleStory SEA --> View below for screenshot

Click Here for more Information!

Image
NoobHacker
Headmaster of Darkness
Headmaster of Darkness
Posts: 576
Joined: Tue Dec 29, 2009 12:31 pm

Re: Read text from Text File

Post 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
Alot Imageers in xemectrum!
User avatar
Nerrazzuri
Destiny General
Destiny General
Posts: 1110
Joined: Sun Dec 20, 2009 9:15 pm

Re: Read text from Text File

Post 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. >.<
Selling my ultimate trainer for MapleStory SEA --> View below for screenshot

Click Here for more Information!

Image
NoobHacker
Headmaster of Darkness
Headmaster of Darkness
Posts: 576
Joined: Tue Dec 29, 2009 12:31 pm

Re: Read text from Text File

Post 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?
Alot Imageers in xemectrum!
User avatar
Nerrazzuri
Destiny General
Destiny General
Posts: 1110
Joined: Sun Dec 20, 2009 9:15 pm

Re: Read text from Text File

Post 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
Selling my ultimate trainer for MapleStory SEA --> View below for screenshot

Click Here for more Information!

Image
Post Reply