[.net] Strange problem with loading files

Started by
2 comments, last by supercoder74 18 years, 9 months ago
ok, this is really strange. I made a simple text editor(all it is is a rich txt box,open button,and save button). I can save normally, and I can read text files I created using notepad normally. One problem though: I can read text files created using notepad, wordpad, ect, but I cannot read text files created by my program! When I try to open files created by this program, it only reads the first character. Heres my open file routine:

//class variables, only listing the ones that matter.
private System.Windows.Forms.RichTextBox richTextBox1;
		private System.Windows.Forms.SaveFileDialog saveFileDialog1;
		private System.Windows.Forms.OpenFileDialog openFileDialog1;
		private System.Windows.Forms.Button button2;
void Button2Click(object sender, System.EventArgs e)
		{
			
			if(openFileDialog1.ShowDialog()==DialogResult.OK)
			{
				if((openFileDialog1.OpenFile())!=null)
				{
					StreamReader str=new StreamReader(openFileDialog1.FileName);
					richTextBox1.Text=str.ReadToEnd();
					str.Close();
				}
			}
		}



and here is my save file routine:


void Button1Click(object sender, System.EventArgs e)//button1 is also declared.
		{
			Stream s;
			if(saveFileDialog1.ShowDialog()==DialogResult.OK)
			{
				if((s=saveFileDialog1.OpenFile())!=null)
				{
					
					byte[] arr=new byte[5000];
					arr=stringtobyte(richTextBox1.Text);
				    int size=arr.Length;
				    s.Write(arr,0,size);
				    s.Close();
		               }
			}
			
		}



so whats my problem? EDIT: I know the indenting is kinda screwed, sharpdevelop does not copy very well. [Edited by - supercoder74 on June 29, 2005 1:36:54 PM]
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.
Advertisement
I assume your call stringtobyte just copies? Did you think about encoding UTF stuff and such?

Check out The System.Text.Encoding class.

Cheers
I'd redo your save function:
byte[] arr=new byte[5000];arr=stringtobyte(richTextBox1.Text);int size=arr.Length;s.Write(arr,0,size);s.Close();

To something like (off the top of my head):
StreamWriter str = new StreamWriter( saveFileDialog1.OpenFile() );str.Write( richTextBox1.Text );str.close();


Then see if that works better. What can happen with your method is that if a 0 byte is stored, that is translated to a EOF, so your program will only read in one character. In other text programs though, it is not a problem since it reads the entire file.
Thanks! it worked. One problem with your code though: you cannot directly use
openFileDialog1.OpenFile() on the new streamwriter line, because then the text file is being used by two processses: openfiledialog and simpletexteditor.exe, which in windows is a no-no.
to solve this, I used this code:
void Button1Click(object sender, System.EventArgs e)		{			Stream s;			if(saveFileDialog1.ShowDialog()==DialogResult.OK)			{				if((s=saveFileDialog1.OpenFile())!=null)				{									StreamWriter str = new StreamWriter(s);                                str.Write( richTextBox1.Text );                                str.Close();		               }			}					}

I had to use a temporary stream to make sure its only one process, not two.
EDIT: more screwed indenting.
I program in my sleep,but when I sleep I use the partition in my head that doesnt have g++ or the .net library, so im kinda screwed.

This topic is closed to new replies.

Advertisement