Sign in to follow this  
supercoder74

[.net] Strange problem with loading files

Recommended Posts

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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this