tags:

views:

1989

answers:

4

Hi all,

I'm using the textwriter to write data to a text file but if the line exceeds 1024 characters a line break is inserted and this is a problem for me. Any suggestions on how to work round this or increase the character limit?

textWriter.WriteLine(strOutput);

Many thanks

+5  A: 

Use Write, not WriteLine

DrG
+4  A: 

Well you're using TextWriter.WriteLine(string) which appends \r\n after strOutput. As the docs say:

Writes a string followed by a line terminator to the text stream.

(Emphasis mine.) That has nothing to do with 1024 characters though - my guess is that that's how you're reading it in (e.g. with a buffer of 1024 characters).

To avoid the extra line break, just use

textWriter.Write(strOutput);

EDIT: You say in the comment that you need a line break after "the full line has been written out" - but it sounds like strOutput isn't always the same line.

I suspect the easiest way of accomplishing what you want is to separate the "copying" side out from the "line break" side. Use Write for all the text you want to copy, and then just call

textWriter.WriteLine();

when you want a line break. If this doesn't help, I think we're going to need more context - please provide a code sample of exactly what you're doing.

Jon Skeet
Yeah, this seems to be exactly the issue. There's certainly nothing intrinsic to the TextWriter/StreamWriter class that imposes a 1024 (or any) limit.
Noldorin
Thank Jon, I need the line break inserting after the full line has been written using .Write(strOutput); what would be the best way to do this? Thanks
I edited my answer to reflect that.
Jon Skeet
Thanks Jon, darn notepad limitation! Thanks for the help.
A: 

if you need to add the line breaks at the end of your output just append them.

textWriter.Write(strOutput +"\r\n");
benjamin
+1  A: 

I wrote a sample app that writes and read a 1025 character string. The size never changes. Although if I opened it with notepad.exe (Windows) I can see the extra character in the second line. These seems like a notepad limitation. Here is my sample code

    static void Main(string[] args)
    {
        using (TextWriter streamWriter = new StreamWriter("lineLimit.txt")) {
            String s=String.Empty;
            for(int i=0;i<1025;i++){
                s+= i.ToString().Substring(0,1);
            }
            streamWriter.Write(s);
            streamWriter.Close();
        }
        using (TextReader streamReader = new StreamReader("lineLimit.txt"))
        {
            String s = streamReader.ReadToEnd();
            streamReader.Close();
            Console.Out.Write(s.Length);
        }
    }
Igor Zelaya
Yup, this could well be it - in other words, the problem didn't exist in the first place :)
Jon Skeet
It was a notepad limitation, notepad2 shows it perfectly, thanks for all your time. Much appreciated.