growth of .srt file after being copied . . .

hey to you all,

i have some .srt files in hebrew and my subtitels reading dvd connects them reversed so i built a script to reverse the text rows in the srt file and leave the time and data row untouched. i open the original file using streamreader and writes it reversed to another file using streamwriter but the reversed file is bigger then the original. also if i convert a filed that's been already converted the text becomes jibrish. the encoding i used to read the file system.text.encoding.defauld cause its the only one who worked.

thanx for the help,
this is the code:

using System;
using System.IO;
using System.Text;
using System.Collections;
namespace ConsoleApplication5
{
 class Class1
 {
  [STAThread]
  static void Main()
  {
   string[] allFilesInFldr=Directory.GetFiles(Directory.GetCurrentDirectory());
   for (int i = 0; i < allFilesInFldr.Length; i++)
   {
    if(!allFilesInFldrIdea.EndsWith(".srt"))
     allFilesInFldrIdea="";
   }
   for (int i = 0; i < allFilesInFldr.Length; i++)
   {
    if(allFilesInFldrIdea!="")
    {
     StreamReader fileReader=new StreamReader(File.OpenRead(allFilesInFldrIdea), System.Text.Encoding.Default);
     StreamWriter fileWriter=new StreamWriter(allFilesInFldrIdea.Replace(".srt", "-reversed.srt"));
     while(fileReader.Peek()!=-1)
     {
      if((fileReader.Peek()>57)||(fileReader.Peek()<48))//
      {
       string line=fileReader.ReadLine();
       char[] lineCharArray=line.ToCharArray();
       char[] reversed=new char[lineCharArray.Length];
       for (int cntr=0;cntr<lineCharArray.Length;cntr++)//
       {
        reversed[cntr]=lineCharArray[lineCharArray.Length-cntr-1];
       }
       StringBuilder fromCharArrayToString=new StringBuilder();//
       for (int cntr = 0; cntr <reversed.Length ; cntr++)
       {
        fromCharArrayToString.Append(reversed[cntr]);
       }
       fileWriter.WriteLine(fromCharArrayToString);
      }
      else
      {
       fileWriter.WriteLine(fileReader.ReadLine());
      }
     }
     fileWriter.Close();
     fileReader.Close();
    }
   }
  }
 }
}


Answer this question

growth of .srt file after being copied . . .

  • Vigilante

    I'm not familiar with the .srt format but if this is just plain text then you might want to consider opening the file as binary and maniuplate it this way. If I need to guess and I could be wrong it's a single byte encoding.

    The problem you are having is that you are reading the text assuming it's ANSI encoded (using the system default codepage) but while reading the stream the characters are converted to unicode then you saving the information back as unicode. This would explain why the size of the file is bigger and also why the .srt reader gets confused.

    btw: You code has other assumptions that you might want to consider while reversing the string. If the string contains English or numbers inside the string then might want to skip them. If the encoding is logical what you are trying to really do is convert a string from logical to visual.


  • EdSF

    you should use the same encoding with the StreamWriter, like this:

    StreamWriter fileWriter=new StreamWriter(allFilesInFldr.Replace(".srt", "-reversed.srt"),false,System.Text.Encoding.Default);


  • growth of .srt file after being copied . . .