Brijesh Mishra
Brijesh Mishra

Reputation: 2748

C# notepad alike file read

I have a file. When I open it in Notepad I see charachters in a single line. However, if I open that file in any other application like WordPad or Notepad++. I can see irregular line breaks between charachters and they appear in multiple lines. These line breaks also appears if I do reader.Readline(). How can I perform Notepad like line read in C#?

Upvotes: 1

Views: 2675

Answers (4)

Cesar Sanz
Cesar Sanz

Reputation: 1

Use

String[] lines = File.ReadAllLines("c:\\text.txt");

To extract all lines in one shot

Upvotes: 0

Sergej Andrejev
Sergej Andrejev

Reputation: 9413

If this is not that critical you could just load all text and split it into lines using a regular expression or something,

var path = "c:/test.txt";

File.AppendAllText(path, "lala\nlala\nlalal");
var text = File.ReadAllText(path);

Console.WriteLine(Regex.Split(text, Environment.NewLine).Length);

Upvotes: 0

Sergej Andrejev
Sergej Andrejev

Reputation: 9413

Sorry for second answer from me, but I just realised that you could use peek to get next character and check if current and next are \r\n, like so:

var path = "c:/test.txt";

File.WriteAllText(path, "a\nb\r\nc");

using (var stream = File.OpenRead(path))
using (var reader = new StreamReader(stream, Encoding.ASCII))
{
    var lineBuilder = new StringBuilder();
    string line;
    char currentChar;
    int nextChar;
    while (!reader.EndOfStream)
    {
        currentChar = (char)reader.Read();
        nextChar = reader.Peek();

        if (!(currentChar == '\r' && nextChar == '\n'))
        {
            lineBuilder.Append(currentChar);
        }

        if((currentChar == '\r' && nextChar == '\n') || nextChar == -1)
        {
            line = lineBuilder.ToString();
            Console.WriteLine(line);

            lineBuilder = new StringBuilder();

            reader.Read();
        }
    }
}

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1064114

Such differences are usually because of ambiguity over which encoding the file uses. If you want the file top parse correctly, you'll need to use the right encoding. What characters are causing problems? Also - if the bytes aren't legal in the encoding used, all bets are off ;-p

You can specify the encoding when creating (for example) a StreamReader over the file:

    using (Stream stream = File.OpenRead(path))
    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
    {
        string line;
        while ((line = reader.ReadLine()) != null)
        {
            Console.WriteLine(line);
        }
    }

With the base-64 posted, the problem is the \r in the middle of the line. To read this as a basic string, you could use:

 byte[] data = File.ReadAllBytes(path);
 string txt = Encoding.UTF8.GetString(data);

or just:

 string text = File.ReadAllText("foo.txt");

However, you'll always have difficulty displaying this. You can now Split on your chosen line ending (crlf presumably). StreamReader is splitting on anything that looks like a line ending.

string[] lines = s.Split(new string[] {"\r\n"}, StringSplitOptions.None);

Upvotes: 4

Related Questions