2

I am writing a text into a file. I am getting a column from database and stored it in a string and writing it in a text file. That column contains c# code and it is writing like a single line with small squares for next line(space). I need to write it line by line. Here is my code.

using (var dest = File.AppendText(Path.Combine(_logFolderPath, "a.txt")))
{
    dest.WriteLine(line.TrimStart());
}  

Any suggestion?

Furqan Safdar
  • 16,260
  • 13
  • 59
  • 93
bala3569
  • 10,832
  • 28
  • 102
  • 146
  • 1
    what is the question? you are storing C# code in a database? i am totally confused :P – tenfour Nov 30 '10 at 10:35
  • whats wrong with this code ? it already writes line by line – Ali Tarhini Nov 30 '10 at 10:36
  • its writing like first line then small square and then next line in that same line itself – bala3569 Nov 30 '10 at 10:36
  • if your question is about removing the line ending characters, then see this post http://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames – thumbmunkeys Nov 30 '10 at 10:38
  • The problem is that you're opening it in Notepad. – Ignacio Vazquez-Abrams Nov 30 '10 at 10:38
  • @Ignacio: Why? What does it do wrong? For many things (e.g. File.WriteAllBytes) it's the simplest way of doing the job, and it does it correctly as far as I'm aware. – Jon Skeet Nov 30 '10 at 10:38
  • The problem is windows vs linux style of newlines (\n vs \r\n) – Dominik Seibold Nov 30 '10 at 10:39
  • @Jon: I guess it mostly has to do with me. It smells to me of magic, but I'm sure there's a perfectly good explanation for it. – Ignacio Vazquez-Abrams Nov 30 '10 at 10:41
  • @Ignacio Ya i am opening it in notepad...if i want a particular line means it is getting the complete line – bala3569 Nov 30 '10 at 10:44
  • @bala3569: There are many, many free text editors that will display the file properly. – Ignacio Vazquez-Abrams Nov 30 '10 at 10:45
  • @Ignacio: It's not magic - it's just useful utility code to stop you having to write the same code again and again. – Jon Skeet Nov 30 '10 at 10:45
  • @Ignacio I am writing it into a text file and i have to search it with a keyword and return that line..but it returns the complete line – bala3569 Nov 30 '10 at 10:45
  • @Ignacio Displaying things like that is not my problem.I have to write twice thats my problem... – bala3569 Nov 30 '10 at 10:57
  • @Jon Skeet:See the thing is first i am wring some codes into a text file...after that i am searching a particular line from that text file...but its returning the whole line instead of a particular line..This is my problem... – bala3569 Nov 30 '10 at 11:03
  • @bala3569 please open the file "a.txt" with hex viewer and see what is the HEX value of those squares. You'll have to replace this with "real" newline character or remove it, depending on what you need. – Shadow The GPT Wizard Nov 30 '10 at 12:48
  • @Shadow Wizard that square is 0D – bala3569 Nov 30 '10 at 13:52
  • @bala3569 - this is "carriage return" character, "\r" as far as I know.. so this code should finally work: `dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));` - "\n" is "line feed" character which is 0A in the ASCII table. – Shadow The GPT Wizard Nov 30 '10 at 13:58

3 Answers3

4

Does Notepad show small squares for new lines, but when you look at the file in Visual Studio it's OK? If so, my guess is that this will fix it:

dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine));
Tim Robinson
  • 53,480
  • 10
  • 121
  • 138
  • +1 - Though since the dest.WriteLine method appends a line feed character of its own, you might want to Replace the "\n" characters with string.Empty. – sheikhjabootie Nov 30 '10 at 10:43
1

try this

File.AppendAllText(Path.Combine(_logFolderPath, "a.txt", 
    line.Trim() + Environment.NewLine));
Dean Chalk
  • 20,076
  • 6
  • 59
  • 90
1
dest.WriteLine(line.TrimStart().Replace("\n", Environment.NewLine).Replace("\r", Environment.NewLine));
bala3569
  • 10,832
  • 28
  • 102
  • 146