int j = (1024 * 1024); // = 1 megabyte
char[] buffer = new char[j];
int charsRead = 0;
while ((charsRead = sr.Read(buffer, 0, buffer.Length)) > 0)
{
string john = new string(buffer, 0, charsRead);
sw.WriteLine(john);
}
This is my first experience with using a buffer, and the above code does what I want, EXCEPT for the fact that the end of the buffer does not coincide with the end of the lines in the text file being read from. This results in what you see below. Keep in mind that because each line in the source file is potentially a different length, the break doesn’t always occur in the same location in the line:
john likes to farm cattle
john likes to farm beetles
john likes to farm rabbits
john likes to farm carrots
john likes to farm b <---1MB buffer ends here
ears <---new 1MB buffer begins here
john likes to farm antelope
john likes to farm rabies
john likes to farm lions
So is there a way to have a buffer of a specified size (1MB in this example), but only up to the end of the last line before 1MB is reached (so the buffer would most likely always be slightly less than 1MB in size)? I’m guessing part of that process would involve defining what exactly a line is (luckily I know how to do this now), but after that I don’t know what I would need to do.
The only solution I can think of would be to go through after the contents of the buffer have been written to the file and search for incomplete lines and re-join them with the lines they were broken from. This seems really inefficient though.
edit: I forgot to include the format of the source file being read from:
john likes to farm cattle
john likes to farm beetles
john likes to farm rabbits
john likes to farm carrots
john likes to farm bears
john likes to farm antelope
john likes to farm rabies
john likes to farm lions
The most obvious solution (in my opinion) would be to have the strings in your buffer contain the newline (and keep it when they are read) and use
Writeinstead ofWriteLine.