none
how to insert a carriage return every 250 chars in a huge .txt file. text file is 7 meg RRS feed

  • Question

  • I have a huge 7 meg .txt file with no carriage returns in it.  I need to move through the .txt file

    and insert a carriage return every 250 characters in the .txt file where the carriage return will

    be added in position 251 throughout the file.  This will allow me to do further processing of each "line"

    within the file.   As an alternative I would also like to move through the file grabbing 250 characters

    each time and outputting the resulting "records" to another .txt file.

    Tuesday, August 14, 2018 12:38 PM

All replies

  • TextReader has ReadBlock method. You can read 250 chars with one block and write it into output file and then write CR char after each block.
    Tuesday, August 14, 2018 1:09 PM
  • Hi joeoloughlin,

    You can try reading the text form your text file and use the String.Insert Method to add the carriage return in 251 position.


    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered"Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]


    Tuesday, August 14, 2018 1:20 PM
    Moderator
  • I need to move through the .txt file

    and insert a carriage return every 250 characters in the .txt file 

     As an alternative I would also like to move through the file grabbing 250 characters

    each time and outputting the resulting "records" to another .txt file.

    You can try something along these lines (E&OE):

    static void Main(string[] args)
     {
         string pathout = "OutFile.txt";
         string pathin = "InFile.txt";
    
         try
         {
             if (File.Exists(pathout))
             {
                 File.Delete(pathout);
             }
    
             if (!File.Exists(pathin))
             {
                 Console.WriteLine("Input file not found!");
                 Console.ReadLine();
                 return;
             }
    
             using (StreamWriter sw = new StreamWriter(pathout))
             {
                 using (FileStream fs = new FileStream(pathin, FileMode.Open))
                 {
                     using (StreamReader sr = new StreamReader(fs))
                     {
                         char[] c = new char[250];
    
                         int count = 0;
                         while ((count = sr.Read(c, 0, c.Length)) > 0)
                         {
                             sw.Write(c, 0, count);
                             sw.WriteLine();
                         }
                     }
                 }
             }
         }
         catch (Exception e)
         {
             Console.WriteLine("The process failed: {0}", e.ToString());
         }
     }
    

    Note that it writes a newline (CRLF = 0x0D0A) after every 250 characters.
    Or less if EOF occurs before 250 chars are read. If you want just a CR
    then amend the code accordingly.

    - Wayne

    Tuesday, August 14, 2018 2:53 PM
  • hello, 

    here is another method to do so, you can adapt the line length and the string to use as separator : 

    public static async Task ReadFileAndAppendSeparator(string filePath, string separator = "\r\n", int lineLength = 250)
    {
        byte[] result = Encoding.ASCII.GetBytes(separator);
        using (FileStream SourceStream = File.Open(filePath, FileMode.OpenOrCreate))
        {
    
            SourceStream.Seek(0, SeekOrigin.Begin);
    
            while (SourceStream.Position < (SourceStream.Length - lineLength))
            {
                SourceStream.Seek(lineLength, SeekOrigin.Current);
                await SourceStream.WriteAsync(result, 0, result.Length);
                        
                        
            }
        }
    }

    and to use the method :

    // just specify the filePath, separator and line length 
    FileStreamExamples.ReadFile(@"userinputlog.txt", "\r\n", 250);

    good coding;


    Friday, August 17, 2018 6:45 PM