Question

I have a string pattern that has to be overwritten from the second to 207th line of a file.

Currently I have this method to overwrite every 0, 1 or space in the full pattern (which is 207 lines of 3000 length each).

using (var fileStream = File.Open(filePath + fileName, FileMode.OpenOrCreate, FileAccess.Write))
        {
            var minLength = Math.Min(pattern.Length, fileStream.Length);
            Console.WriteLine("Started overwriting file " + fileName);
            for (var i = 0; i < minLength; i++)
            {
                fileStream.Position = i;

                if (pattern[i] == '0')
                {
                    fileStream.WriteByte(byteCharZero);
                }
                else if (pattern[i] == '1')
                {
                    fileStream.WriteByte(byteCharOne);
                }
                else if (pattern[i] == ' ')
                {
                    fileStream.WriteByte(byteCharSpace);
                }
            }
        }

This method works like a charm, till line 110. Then it jumps back one space and on line 161 it does the same thing again. That way the pattern is not in the correct place.

I don't know why this is, but I will approach the problem in another way to solve it:

Now I want to overwrite the second till 207th line with a string pattern that consist of one line. How do I accomplish overwriting separate linse in the method above?

Was it helpful?

Solution

You are talking about "lines", so the first thing to check is the line-endings. Is it, perhaps, that the line-endings are usually CRLF, except for near line 161 where it is just CR or LF? A different line-ending would certainly account for an off-by-one.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top