Hello, i have one project which i made, but i need some help about saving and oppening file..
I have two ListView controls on form.When i click button-save it will save items and subitems from listview to file which have extendion .nk, it's simple .txt file but with .nk extendion.
Here is code for save button:
this text: >!< split string array.
private: void save_project(){
String^ clas_name = class_name_lbl->Text->ToString();
int n,m;
n = listView1->Items->Count;
m = listView2->Items->Count;
String^ ime_fajla = class_name_lbl->Text + "_project";
try
{
saveFileDialog4->FileName = ime_fajla;
if(saveFileDialog4->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
ime_fajla = saveFileDialog4->FileName;
}
StreamWriter^ sw = gcnew StreamWriter(ime_fajla);
sw->Flush();
for(int i=0; i<n ; i++)
{
sw->WriteLine(listView1->Items[i]->Text + ">!<" + listView1->Items[i]->SubItems[1]->Text + ">!<" + listView1->Items[i]->SubItems[2]->Text);
}
sw->WriteLine("+++++");
for(int i=0; i<m; i++)
{
String^ s1;
String^ s2;
if(listView2->Items[i]->SubItems[2]->Text == "" || listView2->Items[i]->SubItems[4]->Text == "")
{
s1 = listView2->Items[i]->SubItems[2]->Text + "#";
s2 = listView2->Items[i]->SubItems[4]->Text + "#";
sw->WriteLine(listView2->Items[i]->Text + ">!<" + listView2->Items[i]->SubItems[1]->Text + ">!<"
+ s1 + ">!<" + listView2->Items[i]->SubItems[3]->Text + ">!<"
+ s2);
}
if(listView2->Items[i]->SubItems[2]->Text != "" || listView2->Items[i]->SubItems[4]->Text != "")
{
sw->WriteLine(listView2->Items[i]->Text + ">!<" + listView2->Items[i]->SubItems[1]->Text + ">!<"
+ listView2->Items[i]->SubItems[2]->Text + ">!<" + listView2->Items[i]->SubItems[3]->Text + ">!<"
+ listView2->Items[i]->SubItems[4]->Text);
}
}
sw->WriteLine("-----");
sw->WriteLine(clas_name);
sw->Flush();
sw->Close();
delete sw;
}
catch(Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}
Here is a code for open project file:
private: void open_project(){
array<String^>^ delitelj = {">!<"};
array<String^>^ podaci_lv1;
array<String^>^ podaci_lv2;
String^ lv1_text;
String^ lv2_text;
int i = 0;
int j = 0;
String^ ime_fajla = "";
try
{
if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
{
ime_fajla = openFileDialog1->FileName;
}
StreamReader^ sr = gcnew StreamReader(ime_fajla);
do{
lv1_text = sr->ReadLine();
array<String^>^ podaci_lv1 = lv1_text->Split(delitelj,3,StringSplitOptions::RemoveEmptyEntries);
listView1->Items->Add(podaci_lv1[0]);
listView1->Items[i]->SubItems->Add(podaci_lv1[1]);
listView1->Items[i]->SubItems->Add(podaci_lv1[2]);
i++;
}while(sr->ReadLine() != "+++++");
do{
if(sr->ReadLine() == "+++++");
{
lv2_text = sr->ReadLine();
array<String^>^ podaci_lv2 = lv2_text->Split(delitelj,5,StringSplitOptions::RemoveEmptyEntries);
listView2->Items->Add(podaci_lv2[0]);
listView2->Items[j]->SubItems->Add(podaci_lv2[1]);
listView2->Items[j]->SubItems->Add(podaci_lv2[2]);
listView2->Items[j]->SubItems->Add(podaci_lv2[3]);
listView2->Items[j]->SubItems->Add(podaci_lv2[4]);
j++;
}
}while(sr->ReadLine() != "-----");
do{
if(sr->ReadLine() == "-----")
class_name_lbl->Text = sr->ReadLine();
}while(!sr->EndOfStream);
sr->Close();
delete sr;
}
catch(Exception^ ex)
{
MessageBox::Show(ex->Message);
}
}
Here is a picture when i enter data in listview controls:
http://i39.tinypic.com/34i2r9s.jpg
Here is a picture when i save file, and how it looks when i open it in Notepad++:
http://i42.tinypic.com/1jql4k.jpg
Here is a picture when i OPEN file in my project software:
http://i39.tinypic.com/2dl2lqd.jpg
If someone know what is the problem and know how to fix it please help me, i will be most grateful..
If you have some links of similar projects and codes post it please.. Thanks!