Click here to Skip to main content
15,892,005 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
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.

C++
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);
				 //lv1
				 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("+++++");			
				 //lv2
				 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("-----");
				 //class name
				 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:


C++
private: void open_project(){
			 //delitelj
			 array<String^>^ delitelj = {">!<"};
			 //podaci za lv1
			 array<String^>^ podaci_lv1;
			 //podaci za lv2
			 array<String^>^ podaci_lv2;
			 //string u koji se ubacuju podaci za lv1
			 String^ lv1_text;
			 //string u koji se ubacuju podaci za lv2
			 String^ lv2_text;
			 //brojaci i,j
			 int i = 0;
			 int j = 0;
			 //ime fajla koji ucitavamo
			 String^ ime_fajla = "";
			 try
			 {
				 if(openFileDialog1->ShowDialog() == System::Windows::Forms::DialogResult::OK)
				 {
					 ime_fajla = openFileDialog1->FileName;
				 }

				 StreamReader^ sr = gcnew StreamReader(ime_fajla);
				 //lv1
				 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() != "+++++");
				 //lv2
				 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() != "-----");
				 //class name
				 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!
Posted

1 solution

Unfortunately I can not see your pictures, but I see some general problem in your parsing code:
C++
//lv1
do{
    lv1_text = sr->ReadLine();
    ...
}while(sr->ReadLine() != "+++++");

Every iteration of the loop reads two lines. The condition in the while always consumes an additional line from your file. If it is not "+++++", the line is still "gone" and the next iteration of the loop will read the following line. In general, every 2nd entry in your file is consumed by the condition check.

A possibility to change your code would be to do this:
C++
//lv1
do{
    lv1_text = sr->ReadLine();
    if(lv1_text == "+++++") { break; }
    ...
}while( true );

This way every line in the file is read inside the loop once and no lines are skipped as before.

The other loops can be fixed in the same way, they also "consume" lines for the loop condition checks.
 
Share this answer
 
v2
Comments
[no name] 20-May-13 7:25am    
Thanks for the respondes i will try this today and i will tell you, i now get it in my head.. Thanks again..
[no name] 20-May-13 9:38am    
Okay i solved for listView1, but there is still problem with listView2, it says this exception in messagebox: Index was outside the bounds of the array? Can you help me with ListView2?

I changed this code:
//lv2
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() != "-----");

TO:
//lv2
do{
lv2_text = sr->ReadLine();
if(lv2_text == "-----")
{
break;
}
else
{
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(true);
[no name] 20-May-13 9:38am    
Thanks!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900