Raphael
Raphael

Reputation: 171

How to insert line break in big text file

i have a big text file (like 1gb) and i need to insert line breaks every 40 characters.

how to do so using streamReader?

i tried using ReadBLock, but cant seem to make it work. Heres the code so far:

int index = 0;
try
{
    using (StreamReader sr = new StreamReader(@"C:\bigfile.txt"))
    {
        char [] buf = new char [1024];
        //sr.ReadBlock (buf, 0, 40);
        while (sr.ReadBlock (buf, index, 40) != 0)
        {
            using (StreamWriter sw = new StreamWriter(@"C:\bigfilelinebreak.txt"))
            {
                //Console.WriteLine(buf);
                sw.Write (buf);
                sw.WriteLine(Environment.NewLine);
            }
            index += 40;
        }

    }
}

Upvotes: 1

Views: 2447

Answers (3)

spender
spender

Reputation: 120420

You're creating a new StreamWriter for every ReadBlock. (I don't know the behaviour of new StreamWriter(@"filename") but I'll wager it's not what you want to happen). Also, the index parameter of ReadBlock is the index into the destination array, not the index into the file. As such it should be 0 for every read. You don't need to consider position in the file as you are streaming from one to another in sequential order. StreamReader/Writer will move the underlying streams on accordingly.

    char [] buf = new char [40];
    int amtRead;
    using (StreamReader sr = new StreamReader(@"C:\bigfile.txt"))
    using (StreamWriter sw = new StreamWriter(@"C:\bigfilelinebreak.txt"))
    while ((amtRead = sr.ReadBlock (buf, 0, 40)) != 0)
    {
        sw.WriteLine (buf, 0, amtRead);
    }

Upvotes: 1

Jon Skeet
Jon Skeet

Reputation: 1500245

Currently you're rewriting the output file on every line. This is clearly not a good plan.

I suspect you want something like this:

using (TextReader reader = File.OpenText(@"c:\bigfile.txt"))
{
    using (TextWriter writer = File.CreateText(@"C:\bigfilelinebreak.txt"))
    {
        char[] buffer = new char[40];
        int charsRead;
        while ((charsRead = reader.ReadBlock(reader, buffer, buffer.Length)) > 0)
        {
            writer.Write(buffer, 0, charsRead);
            writer.WriteLine();
        }
    }
}

Upvotes: 0

Reed Copsey
Reed Copsey

Reputation: 564403

There are a few issues:

  1. You don't need to increment your index - always just write to index zero.
  2. You are recreating your StreamWriter for each block.
  3. You may be writing out the wrong number of characters (you're not checking how many you read, which, at the end of the file, could be less than 40)

Try the following:

    try
    {
        using (StreamReader sr = new StreamReader(@"C:\bigfile.txt"))
        {
            using (StreamWriter sw = new StreamWriter(@"C:\bigfilelinebreak.txt"))
            {
                char [] buf = new char[40];
                int read = sr.ReadBlock (buf, 0, 40);
                while (read != 0)
                {
                    sw.Write(buf, 0, read);
                    sw.WriteLine();
                    read = sr.ReadBlock (buf, 0, 40);
                }
            }
        }
    }

Upvotes: 1

Related Questions