Jump to content
  • Advertisement
Sign in to follow this  
supercoder74

[.net] Strange problem with loading files

This topic is 4766 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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
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

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!