Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8979299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T19:49:09+00:00 2026-06-15T19:49:09+00:00

I have a requirement where the file should be split using a given character.

  • 0

I have a requirement where the file should be split using a given character.
Default splitting options are CRLF,LF and CR.

In these cases I am splitting the line by \r\n and \n and \r respectively.

Also I have requirement where any size of file should be processed.
(Processing is basically inserting the given string in a file at given position).

For this I am reading the file in chunk of 1024 bytes.

Then I am applying the string.Split() method.
Split() method gives options for ignoring white spaces and none.

I have to add back these line break characters to the line.
for this I am using a binary writer and I am writing the byte array to the new file.
Issue:-

1) When line break is CRLF, and the split option is NONE, while spaces are also added in the splitted array. Second option is given (to ignore white spaces) CRLF works properly.

2)Bit ignoring white space option creates other problems, as I am reading the file byte by byte I can’t ignore a white space.

3)When line break characters are other than default(e.g. ‘|’, a null value is prepended to the resulting line.

Can anybody give solution to my issues?

Here is the method I have written

    private static void ProcessOriginalFile(string filePath, string destinationFilePath, int fromPosition, int toPosition, string lineBreakCharacter, string replaceString, long fileSize, bool ignoreHeader, bool ignoreFooter)
    {
        int chunkSize = fileSize < 1024 ? (int)fileSize : 1024;//bytes
        byte[] chunkData = new byte[chunkSize];
        char[] charactersSeparator=new char[1];

        charactersSeparator = CommonOperations.ResolveTheRecordBreak(lineBreakCharacter);           

        int totalLineBreakCharactersLength = 0;
        for (int i = 0; i < charactersSeparator.Length; i++)
        {
            if (charactersSeparator[i] == 0)
                break;
            totalLineBreakCharactersLength = totalLineBreakCharactersLength + BitConverter.GetBytes(charactersSeparator[i]).Length;
        }


            using (BinaryReader fileReader = new  BinaryReader(new FileStream(filePath, FileMode.Open)))
            {
                string lastChunk = string.Empty;
                string chunkcontents = string.Empty;
                IList<byte[]> dataToBeWritten = new List<byte[]>();

                while (fileSize > 0)
                {
                    chunkData = fileReader.ReadBytes(chunkSize);
                    byte[] chunkToBeWritten = new byte[chunkSize];
                    chunkcontents = chunkcontents + System.Text.ASCIIEncoding.UTF8.GetString(chunkData);

                    string[] splittedArray = new string[1];
                        splittedArray = chunkcontents.Split(charactersSeparator, StringSplitOptions.RemoveEmptyEntries);
                        if (ignoreHeader)
                        {
                            splittedArray = splittedArray.Skip(1).ToArray();
                            ignoreHeader = false;
                            if (splittedArray.Count() == 0)
                                continue;
                        }

                    int count = 0;
                    if (splittedArray != null || splittedArray.Count() > 0)
                    {
                        foreach (string str in splittedArray)
                        {
                            ++count;
                            if (count != splittedArray.Length)
                            {
                                string stringToBeEdited = string.Empty;
                                stringToBeEdited = str;
                                if (stringToBeEdited.Length < (toPosition + 1))
                                    throw new Exception("Position exceeds the string length. Line contents are : " + stringToBeEdited + " String length is : " + stringToBeEdited.Length + " To Position is : " + toPosition);
                                //replace the text between from and two positions with the replaced string
                                stringToBeEdited = stringToBeEdited.Remove(fromPosition, toPosition + 1 - fromPosition).Insert(fromPosition, replaceString);

                                //clear the array
                                dataToBeWritten.Clear();
                                AddLineBreakCharacter(lineBreakCharacter, charactersSeparator, totalLineBreakCharactersLength, dataToBeWritten, ref chunkToBeWritten, ref stringToBeEdited);

                                //write data using binary writer
                                WriteBinaryDataToFile(destinationFilePath, dataToBeWritten);
                                totalLinesProcessed++;
                            }
                            else
                            {
                                lastChunk = str;
                                chunkcontents = str;
                            }
                        }
                    }
                    fileSize = fileSize - chunkSize;

                    /*if file size is less than the chunksize*2, then chunk size should be the file size 
                     * and chunkdata array length should be that of the file size*/

                    if (fileSize < (chunkSize * 2))
                    {
                        chunkSize = (int)fileSize;
                        chunkData = new byte[chunkSize];
                    }
                }


                if (!string.IsNullOrEmpty(lastChunk))
                {
                    if (ignoreFooter == false)
                    {
                        if (lastChunk.Length >= toPosition + 1)
                            lastChunk = lastChunk.Remove(fromPosition, toPosition + 1 - fromPosition).Insert(fromPosition, replaceString);
                        else
                            throw new Exception("Position exceeds in the last line of the file. Line contents are : " + lastChunk + " String length is : " + lastChunk.Length + " To Position is : " + toPosition);
                        dataToBeWritten.Clear();
                        byte[] chunkToBeWritten = new byte[chunkSize];
                        AddLineBreakCharacter(lineBreakCharacter, charactersSeparator, totalLineBreakCharactersLength, dataToBeWritten, ref chunkToBeWritten, ref lastChunk);
                        WriteBinaryDataToFile(destinationFilePath, dataToBeWritten);
                        totalLinesProcessed++;
                    }
                }
            }
    }

    private static void WriteBinaryDataToFile(string destinationFilePath, IList<byte[]> chunkToBeWritten)
    {
            using (FileStream fileSream = new FileStream(destinationFilePath, FileMode.Append))
            {
                using (BinaryWriter outfile = new BinaryWriter(fileSream, Encoding.ASCII))
                {
                    foreach (byte[] item in chunkToBeWritten)
                    {
                        outfile.Write(item);
                    }
                }
            }
    }

    private static void AddLineBreakCharacter(string lineBreakCharacter, char[] charactersSeparator, int totalLineBreakCharactersLength, IList<byte[]> dataToBeWritten, ref byte[] chunkToBeWritten, ref string stringToBeEdited)
    {
        switch (lineBreakCharacter)
        {
            case CommonConstants.NEW_LINE:
                stringToBeEdited = stringToBeEdited + CommonConstants.CARRIAGE_RETURN_CHARACTER + CommonConstants.NEW_LINE_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);
                break;

            case CommonConstants.LINE_FEED:
                stringToBeEdited = stringToBeEdited + CommonConstants.NEW_LINE_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                break;
            case CommonConstants.CARRIAGE_RETURN:
                stringToBeEdited = stringToBeEdited + CommonConstants.CARRIAGE_RETURN_CHARACTER;
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                break;
            default:
                chunkToBeWritten = System.Text.Encoding.UTF8.GetBytes(stringToBeEdited);
                //add modified line 
                dataToBeWritten.Add(chunkToBeWritten);

                byte[] seperatorCharArray = new byte[totalLineBreakCharactersLength];
                int destinationIndex = 0;
                foreach (char ch in charactersSeparator)
                {
                    if (ch == 0)
                        break;
                    //convert the character to byte array
                    byte[] charArr = BitConverter.GetBytes(ch);

                    //copy character bytes to seperator character array                        
                    Array.Copy(charArr, 0, seperatorCharArray, destinationIndex, charArr.Length);
                }
                //add line break characters array to list
                dataToBeWritten.Add(seperatorCharArray);
                break;
        }
    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T19:49:11+00:00Added an answer on June 15, 2026 at 7:49 pm

    I have solved the issue. I have simply dumped above code and used TextReader to read the file character by character. Simultaneously I have checked for line break character. As soon as I got it, I have processed the line.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In a web project using jsp, I have following requirement Upload a file (say
I have a requirement to log to different log file using Nlog based different
I have a requirement to write HTML to the file system and I was
I have a requirement to export a dataset as a CSV file. I have
I have one requirement that i have to place the log file in the
I have a requirement to allow users to browse directories on our file server,
I have got the requirement to find the core file in multiple box/machine and
I have a specific requirement to read a resource from jar file and then
I have a requirement to download a PDF file and saving it on clicking
I have a requirement to hand-code an text file from data residing in a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.