StreamWriter loses format of existing text?


G

Guest

Hello,

My code is suppose to write to an existing file. But after my C# code
appends the file, the previous text loses all the endline characters,
becoming one long line. How can I retain the formating of the pre-existing
text?

FileStream stream = new FileStream(strFileName, FileMode.Append,
FileAccess.Write);

StreamWriter writer = new StreamWriter(stream);

writer.WriteLine(strErrorMessage);

Thanks in advancefor any help....

Duckwon
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Duckwon said:
My code is suppose to write to an existing file. But after my C# code
appends the file, the previous text loses all the endline characters,
becoming one long line. How can I retain the formating of the pre-existing
text?

FileStream stream = new FileStream(strFileName, FileMode.Append,
FileAccess.Write);

StreamWriter writer = new StreamWriter(stream);

writer.WriteLine(strErrorMessage);

Thanks in advancefor any help....

You haven't shown how you're constructing strErrorMessage in the first
place.

Could you post a short but complete program which demonstrates the
problem?

See http://www.pobox.com/~skeet/csharp/complete.html for details of
what I mean by that.
 
G

Guest

At the moment its just a simple

string strErrorMessage = dCurrentTime.Date + " " + dCurrentTime.TimeOfDay +
": " + strFile + ", Line " + nLine.ToString() + ": " + strError;

strFile and strError are also strings with test text such as "Test"

The file with the pre-existing text that I am trying to append, was created
and previously written to in C++. Is this maybe an encoding problem? If so,
how would I go about fixing it?

Thanks for helping
 
J

Jon Skeet [C# MVP]

Duckwon said:
At the moment its just a simple

string strErrorMessage = dCurrentTime.Date + " " + dCurrentTime.TimeOfDay +
": " + strFile + ", Line " + nLine.ToString() + ": " + strError;

strFile and strError are also strings with test text such as "Test"

The file with the pre-existing text that I am trying to append, was created
and previously written to in C++. Is this maybe an encoding problem? If so,
how would I go about fixing it?

It's hard to say. It could be, or it could be that the C++ code is
using a different end of line string.

Could you mail me with a sample file, both before and after?
 
W

Willy Denoyette [MVP]

Duckwon said:
Hello,

My code is suppose to write to an existing file. But after my C# code
appends the file, the previous text loses all the endline characters,
becoming one long line. How can I retain the formating of the pre-existing
text?

FileStream stream = new FileStream(strFileName, FileMode.Append,
FileAccess.Write);

StreamWriter writer = new StreamWriter(stream);

writer.WriteLine(strErrorMessage);

Thanks in advancefor any help....

Duckwon



This is very unlikely, pre-existing data in a file opened in Append mode, cannot be changed,
it's not possible to write before the current file pointer. What are you using to inspect
the file contents, before and after appending?

Willy.
 
Ad

Advertisements

G

Guest

I have a breakpoint before the C# code. I check the text file using notepad
and the formatting is correct. Once the C# writes to the file, the C++ text
has lost its formating while the appended text has the correct formatting.
I'm currently running with 'it's an ecoding issue' theory, but everthing I've
tried hasn't made a differance.
 
J

Jon Skeet [C# MVP]

Duckwon said:
I sent you the two files. The C++ code should be using "\n"

Hmm. That's very odd - the files are very different indeed.
The file Before.txt is written in UTF-16, with a BOM, whereas the file
After.txt appears to be ASCII with alternate bytes being *spaces*
(rather than 0s).

For a start, if you're trying to append to a UTF-16 file, you ought to
open your StreamWriter using UTF-16 (Encoding.Unicode). But I can't see
what's changed the files so much, unless (as is quite possible) they've
been corrupted in the course of mailing them.

If you want me to look further, you could zip the files up and mail a
zip of them... but hopefully the above will fix the problem.
 
W

Willy Denoyette [MVP]

Duckwon said:
I have a breakpoint before the C# code. I check the text file using notepad
and the formatting is correct. Once the C# writes to the file, the C++ text
has lost its formating while the appended text has the correct formatting.
I'm currently running with 'it's an ecoding issue' theory, but everthing I've
tried hasn't made a differance.

Weird, A file opened in Append mode should not be altered.
Say you have file in which each line is terminated with LF (or CR), then notepad will show
one single line, as notepad looks for a sequence of CR LF or LF CR as line terminating
characters, LF or CR alone will not get interpreted as an EOL sequence. That means that,
when notepad shows the contents of the file "correctly", before the program runs, that this
should remain like this after you have appended some test. If the lines in the file where
terminated with CR or LF only then the contents of the file would be wrong before the
program already ran, the appended text should be shown as expected though.

So IMO, you have an input file terminated with only a LF (or CR) and notepad shows the file
as having a single line to begin with. Mind to look at the file contents using a binary
editor?

Willy.
 
G

Guest

Willy Denoyette said:
Weird, A file opened in Append mode should not be altered.
Say you have file in which each line is terminated with LF (or CR), then notepad will show
one single line, as notepad looks for a sequence of CR LF or LF CR as line terminating
characters, LF or CR alone will not get interpreted as an EOL sequence. That means that,
when notepad shows the contents of the file "correctly", before the program runs, that this
should remain like this after you have appended some test. If the lines in the file where
terminated with CR or LF only then the contents of the file would be wrong before the
program already ran, the appended text should be shown as expected though.

So IMO, you have an input file terminated with only a LF (or CR) and notepad shows the file
as having a single line to begin with. Mind to look at the file contents using a binary
editor?

Willy.

Thanks Willy, but Jon (above) figured it out. This is my first C# project
and I've got a lot to learn. =-)

Duckwon
 
W

Willy Denoyette [MVP]

Duckwon said:
I have a breakpoint before the C# code. I check the text file using notepad
and the formatting is correct. Once the C# writes to the file, the C++ text
has lost its formating while the appended text has the correct formatting.
I'm currently running with 'it's an ecoding issue' theory, but everthing I've
tried hasn't made a differance.

Ok, I see from Jon's reply that your original file is UTF-16 encode, and you are writing to
the file using another encoding, that means that the appended text will appear incorrectly
in Notepad after running the program, but the original text should still appear as it was,
the system *may not touch* the file contents before EOF when opening in Append mode. Are you
sure you haven't touched the file using an editor that has changed the BOM and the
formatting?.

Willy.
 
Ad

Advertisements

G

Guest

Jon Skeet said:
Hmm. That's very odd - the files are very different indeed.
The file Before.txt is written in UTF-16, with a BOM, whereas the file
After.txt appears to be ASCII with alternate bytes being *spaces*
(rather than 0s).

For a start, if you're trying to append to a UTF-16 file, you ought to
open your StreamWriter using UTF-16 (Encoding.Unicode). But I can't see
what's changed the files so much, unless (as is quite possible) they've
been corrupted in the course of mailing them.

If you want me to look further, you could zip the files up and mail a
zip of them... but hopefully the above will fix the problem.

Thanks Jon! That worked! Everything is once more as it should be, order in
the universe is retored! Thanks again!

Duckwon

P.S. Yes the files really were that different.
 
G

Guest

Willy Denoyette said:
Ok, I see from Jon's reply that your original file is UTF-16 encode, and you are writing to
the file using another encoding, that means that the appended text will appear incorrectly
in Notepad after running the program, but the original text should still appear as it was,
the system *may not touch* the file contents before EOF when opening in Append mode. Are you
sure you haven't touched the file using an editor that has changed the BOM and the
formatting?.

Willy.

Nope, only Notepad. I could open and close the file without the formating
getting changed. Until my C# code wrote to it. Using StreamWriter without the
"Encoding.Unicode" parameter definately caused the pre-existing text to lose
it's formating. When I added the parameter, the file retained the formating.
 
J

Jon Skeet [C# MVP]

Duckwon said:
Nope, only Notepad. I could open and close the file without the formating
getting changed. Until my C# code wrote to it. Using StreamWriter without the
"Encoding.Unicode" parameter definately caused the pre-existing text to lose
it's formating. When I added the parameter, the file retained the formating.

But did you save the "after" file in Notepad before sending it to me?
If so, that would explain it.

I'll try to reproduce it...
 
W

Willy Denoyette [MVP]

Duckwon said:
Nope, only Notepad. I could open and close the file without the formating
getting changed. Until my C# code wrote to it. Using StreamWriter without the
"Encoding.Unicode" parameter definately caused the pre-existing text to lose
it's formating. When I added the parameter, the file retained the formating.


You must have saved in notepad using a different encoding, the pre-existing contents of a
file is not affected when opening the file in append mode even not when using a different
encoding.

Willy.
 
G

Guest

Jon Skeet said:
But did you save the "after" file in Notepad before sending it to me?
If so, that would explain it.

I'll try to reproduce it...

I did a "Save As" to change the name. But I assure you when I open the 'Save
as' After.txt it looks the same as before I saved it. The C++ text always
looked that way after the C# code wrote to the file. I saw this behavior for
two days now without ever saving the file. Code wise, obviously the C++ code
saved the file to disk, as did the C# sharp code when it closed the writer
and/or stream. But I never did until I made a copy to send to you, and that
'After' file looks like all the previous text files after the call to
StreamWriter.
 
Ad

Advertisements

G

Guest

Willy Denoyette said:
You must have saved in notepad using a different encoding, the pre-existing contents of a
file is not affected when opening the file in append mode even not when using a different
encoding.

Willy.

I never saved in Notepad, as I was just using it to view the file (I did
save a copy to send to Jon:explained above). Code wise, obviously the C++
code saved the file to disk, as did the C# sharp code when it closed the
writer and/or stream. But I never personally saved it. I wish I could make
you guy believe me =-)
 
J

Jon Skeet [C# MVP]

Duckwon said:
I did a "Save As" to change the name.

That's almost certainly the problem then.
But I assure you when I open the 'Save as' After.txt it looks the
same as before I saved it.

Yes, but that's *after* Notepad has interpreted the file. Try to
reproduce the problem (using a hex viewer like frhed to examine the
file) *without* bringing notepad into the equation. I'm 99.9% sure you
won't see the file change other than the data being appended at the
end. I certainly couldn't reproduce it.
The C++ text always looked that way after the C# code wrote to the
file. I saw this behavior for two days now without ever saving the
file. Code wise, obviously the C++ code saved the file to disk, as
did the C# sharp code when it closed the writer and/or stream.

No - the C# code just appended to the file. It didn't rewrite all the
data from the start. That's not the same as saving from Notepad, which
will rewrite all the data each time you save.
But I never did until I made a copy to send to you, and that
'After' file looks like all the previous text files after the call to
StreamWriter.

Change the name of the file using explorer instead, and I'm positive
you'll find that the part of the file that C++ wrote out to start with
will be untouched.
 
J

Jon Skeet [C# MVP]

Duckwon said:
I never saved in Notepad, as I was just using it to view the file (I did
save a copy to send to Jon:explained above). Code wise, obviously the C++
code saved the file to disk, as did the C# sharp code when it closed the
writer and/or stream. But I never personally saved it. I wish I could make
you guy believe me =-)

I believe that notepad displayed the file annoyingly. I don't believe
that the file you sent me is the same as it was directly after the C#
code had appended to it.
 
Ad

Advertisements

G

Guest

Jon Skeet said:
I believe that notepad displayed the file annoyingly. I don't believe
that the file you sent me is the same as it was directly after the C#
code had appended to it.

I sent you zipped copies of the before and after file. I zipped the files
without ever opening them… so they are untouched. After that I opened the
'after' text file in a couple of other editors (TextPad and Word). In those
cases the text kept its format. There is a font difference between the C++
text and the C# text. I would prefer the text to be seamless.

When I open the same file in Notepad I still get the unformatted C++ text
(the first portion).

This was with my original code, without your "Encoding.Unicode" fix.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top