Hi guys
i have a problem that i have a text file which contains very long text like :
welcome to all, how are you doing, etc..

so i need to cut the text till each comma and put it in a separate file like i want to pu
[welcome to all,] in a separate file and put
[ how are you doing,] in a separate file and so on
so please if any one can help me in this i would appreciate it a lot
i know a little of c++,c#,vb.net
so any code in any of this lang. would be great

Thank you in advance

Here is a quick-n-dirty version:

using System;
using System.IO;

namespace DW_390448
{
   class Program
   {
      private static void Usage()
      {
         Console.Write("DW_390448 infile outfile");
      }

      static void Main(string[] args)
      {
         if (!args.Length.Equals(2))
         {
            Usage();
            return;
         }

         try
         {
            using (StreamReader fileIn = new StreamReader(args[0]))
            {
               using(StreamWriter fileOut = new StreamWriter(args[1]))
               {
                  string[] arr_strData = {};

                  while(!fileIn.EndOfStream)
                  {
                     arr_strData = fileIn.ReadLine().Split(',');
                     foreach(string s in arr_strData)
                     {
                        fileOut.WriteLine(s.Trim());
                     }
                  }
                  //
                  fileOut.Close();
               }
               //
               fileIn.Close();
            }
         }
         catch (Exception exc)
         {
            Console.WriteLine("Exception: " + exc.Message);
            return;
         }

         Console.WriteLine("Finished");
      }
   }
}

Just thinking: If you REALLY meant you wanted each line in a new separate file, there is another method you would need. Here is a program that will do both. If you give it only the input file, it will create multiple output files starting with File000.txt

using System;
using System.IO;

namespace DW_390448
{
   class Program
   {
      private static void Usage()
      {
         Console.WriteLine(
            "DW_390448 infile outfile /*creates outfile*/\n" +
            "DW_390448 infile /*creates multiple outfiles*/\n"
            );
      }

      private static bool SliceFile(StreamReader fileIn, StreamWriter fileOut, ref string strError)
      {
         bool blnRetVal = true;

         try
         {
            using (fileIn)
            {
               using (fileOut)
               {
                  foreach (string s in fileIn.ReadToEnd().Split(",\r\n".ToCharArray()))
                  {
                     fileOut.WriteLine(s.Trim());
                  }
                  //
                  fileOut.Close();
               }
               //
               fileIn.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      private static string GetFileName(int intNameNum)
      {
         return "File" + intNameNum.ToString().PadLeft(3, '0') + ".txt";
      }

      private static bool SeparateFile(StreamReader fileIn, ref string strError)
      {
         bool blnRetVal = true;

         try
         {
            using (fileIn)
            {
               int intFileCount = 0;
               foreach (string s in fileIn.ReadToEnd().Split(",\r\n".ToCharArray(), StringSplitOptions.RemoveEmptyEntries))
               {
                  using (StreamWriter fileOut = new StreamWriter(GetFileName(intFileCount++)))
                  {
                     fileOut.WriteLine(s.Trim());
                     fileOut.Close();
                  }
               }
               //
               fileIn.Close();
            }
         }
         catch (Exception exc)
         {
            blnRetVal = false;
            strError = exc.Message;
         }

         return blnRetVal;
      }

      static void Main(string[] args)
      {
         string strError = "";

         if (!args.Length.Equals(1) && !args.Length.Equals(2))
         {
            Usage();
            return;
         }

         // Split single files into another file
         if (args.Length.Equals(2))
         {
            if (!SliceFile(new StreamReader(args[0]), new StreamWriter(args[1]), ref strError))
            {
               Console.WriteLine("Error: " + strError);
               return;
            }
         }

         // Split single files into multiple files
         if (args.Length.Equals(1))
         {
            if (!SeparateFile(new StreamReader(args[0]), ref strError))
            {
               Console.WriteLine("Error: " + strError);
               return;
            }
         }
         
         Console.WriteLine("Finished");
      }
   }
}
Member Avatar for Nirvin M

Here is yet another stupid code. For simplicity it doesn't checks for errors/exceptions.

static void Main(string[] args)
        {
            ushort count = 1;
            int ch;
            StringBuilder temp = new StringBuilder();

            StreamReader rdr = new StreamReader("input.txt");
            StreamWriter wrt;
            
            while (!rdr.EndOfStream)
            {                
                ch = rdr.Read();
                if (ch != ',')
                    temp.Append((char)ch);
                else
                {
                    wrt = new StreamWriter("op_file_" + (count++) + ".txt");
                    wrt.Write(temp);
                    wrt.Close();
                    temp.Clear();
                }
            }
            wrt = new StreamWriter("op_file_" + count + ".txt");
            wrt.Write(temp);
            wrt.Close();
        }

Thank you all for your answers it helped me a lot Thank you very much guys

Hi i only have one thing else it is how can i make this work in arabic .txt file which is encoded in utf-8

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.