+ Reply to Thread
Results 1 to 4 of 4

Thread: file squeez ?

  1. #1

    file squeez ?

    i have a college project , and the assignment requires that i do a file squeeze function , here is how it works : given file with records and for each record in file if the record has '*' after it then it should be deleted while keeping records with no '*' .

  2. #2
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    here is the complete function , however make a file called : originalfile.txt and put some records on it based on the record format below
    Code:
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.IO;
    
    namespace File_Squeeze
    {
        class Program
        {
            static void Main(string[] args)
            {
                string path = @"originalfile.txt";
                string astrexfile = @"astrex-file.txt"; // the deleted records
                string noastrexfile = @"no-astrex-file.txt"; // rest of records which are valid
    
                //--- Records form : record1*,record2,record3,record4*,record5 ---//
                foreach (string line in File.ReadAllLines(path))
                {
                    // define the spilited data
                    string[] parts = line.Split(',');
                    
                    foreach (string part in parts)
                    {
                        if (part.Contains("*")) // put record on astrex file
                        {
                            StreamWriter sw = File.AppendText(astrexfile);
                            sw.Write(part + " ,");
                            sw.Flush();
                            sw.Close();
                        }
                        else // put record on no-astrex file
                        {
                            StreamWriter sw = File.AppendText(noastrexfile);
                            sw.Write(part + " ,");
                            sw.Flush();
                            sw.Close();
                        }
                    }
                    FileInfo fi = new FileInfo(path);
                    fi.Delete();
                }
            }
        }
    }

  3. #3
    thanks a lot , that`s what i needed , now i got the concept , appreciate your help .

  4. #4
    Junior Member
    Join Date
    Jan 2012
    Posts
    41
    you are more than welcome

+ Reply to Thread

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts