c# - Read text file from resource -
i trying next code split words in text file.
the file written this:
apple"juice"martini lemon"juice"party banana"smoothie"aligns
and code following:
string resource_data = properties.resources.textfile; string[] result = resource_data.split('"'); foreach (string lines in result) { if(combobox1.text == result[0]) { richtextbox2.text = result[2]; } }
taken & edited c++ programme working on worked same txt file.
string^ resource_data = "textfile.txt"; seek { streamreader^ datain = file::opentext(resource_data); string^ datastr; int count = 0; array<string^>^ result; array<char>^ separ = gcnew array<char>{'"'}; while((datastr = datain->readline()) != nullptr) { count++; result = datastr->split(separ); if(combobox1->text == result[0]) // result[0] = name {
what code does.. read each line own. gives first word in each line result[0] sec word on each line result[1] etc. when select word in combobox check if same in text file , line used in result[x]. in c# gives words own result[x] , lines not matter.
how can create next code in c++ work in c# having text file in resources.resx?
i think see problem is. first need split string resource_data separate lines. splitting resource_data on new line character(s):
string[] lines = resource_data.split(new string[1] { environment.newline }, stringsplitoptions.none); foreach (var line in lines) { string[] parts = line.split('"'); if (combobox1.text == result[0]) { richtextbox2.text = result[2]; } }
you using stringreader:
using (stringreader reader = new stringreader(resource_data)) { while (reader.peek() >= 0) { string[] parts = reader.readline().split('"'); if (combobox1.text == result[0]) { richtextbox2.text = result[2]; } } }
additionally if storing path file in resources, open file , read it:
using (streamreader reader = file.opentext(resource_path)) // path file { while (reader.peek() >= 0) { string[] parts = reader.readline().split('"'); if (combobox1.text == result[0]) { richtextbox2.text = result[2]; } } }
c# combobox resources
No comments:
Post a Comment