Page 1 of 1

overwriting a text file with text

Posted: Sat Feb 02, 2008 10:33 am
by Dagger13
well when ever i open a file and it contains a new line or when u press the
enter key that thing
i open files with the openfiledialog

when ever i overwrite a file those new line things come up as a box can somebody help
and im using vb 2005

heres a pic

Image

Posted: Sat Feb 02, 2008 10:44 am
by OwnZ joO
You need to explain better what you are trying to do, and what you did to accomplish it. How did you open the file(StreamWriter??) and what did you do?

Posted: Sat Feb 02, 2008 11:48 am
by Dagger13
maybe now u will understand

Posted: Sat Feb 02, 2008 12:16 pm
by Anthony
looks like a problem with the line terminator,

can you paste some of your saving code?

Posted: Sat Feb 02, 2008 12:25 pm
by Patrickssj6
For saving try this...simple:

Code: Select all

FileOpen(1,*FILEPATHTOSAVETO*, OpenMode.Output)
PrintLine(1, *textfieldcontainingthetext*)
FileClose(1)

Posted: Sat Feb 02, 2008 2:08 pm
by Dagger13
im using visual basic

this is what im using now

My.Computer.FileSystem.WriteAllText(filename, richtextbox1.Text, False)

Posted: Sat Feb 02, 2008 2:15 pm
by Anthony
You can try this...

Code: Select all

richtextbox1.SaveFile(filename, RichTextBoxStreamType.PlainText)

Posted: Sat Feb 02, 2008 2:56 pm
by Prey
It's just the way new lines are being saved by the two different methods.

The FileSystem method uses only the Line Feed (LF) control character, 0x0A, to represent a new line.. whereas notepad expects a Carriage Return (CR) control character, 0x0D, followed by a LF control character to represent a new line..

Without the CR, the LF goes unrecognised by Notepad, thus the unrecognised character..

The method Ant posted will use the CR+LF for new lines, so it'll "work" in Notepad.. but in future you're better off using the StreamWriter class for plain text operations.. in this case:

Code: Select all

        Dim sw As New StreamWriter(s.FileName)
        For i As Integer = 0 To RichTextBox1.Lines.Length - 1
            sw.WriteLine(RichTextBox1.Lines(i))
        Next
        sw.Close()
Although the rtb's built-in save method is obviously much more preferable here.

Posted: Sat Feb 02, 2008 3:01 pm
by Dagger13
lol thnx anthony i didnt even think of that

and thanks everybody else who commented

Posted: Sat Feb 02, 2008 3:34 pm
by Anthony
Yeah no problem.

Next time if I were you, I would go with what prey suggested. That is the way I do it myself but since I figured you were probably new to VB I would help you in the simplest way possible

Posted: Sat Feb 02, 2008 4:16 pm
by Dagger13
im not new ive been on it for a few years i just never thought of doing that

Posted: Sat Feb 02, 2008 10:25 pm
by Patrickssj6
Dagger13 wrote:im using visual basic
My code is also VB even though it looks kinda different :P