Dinesh Santhalingam

Dinesh Santhalingam

  • NA
  • 737
  • 368.7k

File parse in c#

Jan 18 2017 5:21 AM

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.IO;  
  6. namespace readfrom_textfile {  
  7.     class Program {  
  8.         static void Main(string[] args) {  
  9.             var fileStream = new FileStream(@"D:\Practise\test.txt", FileMode.Open, FileAccess.Read);  
  10.             using(var streamReader = new StreamReader(fileStream, Encoding.UTF8)) {  
  11.                 string line;  
  12.                 while ((line = streamReader.ReadLine()) != null) {  
  13.                     string[] data = line.Split(new char[] {  
  14.                         '('  
  15.                     }, StringSplitOptions.RemoveEmptyEntries);  
  16.                     string bikeName = data[0].Trim().Split(' ').Last();  
  17.                     bikeNamebikeName = bikeName.Remove(bikeName.Length - 1);  
  18.                     string[] usageData = data[1].Split(new char[] {  
  19.                         ';'  
  20.                     }, StringSplitOptions.RemoveEmptyEntries);  
  21.                     string issuedBike = usageData[0].Trim().Split(' ')[2];  
  22.                     string inUseBike = usageData[1].Trim().Split(' ')[2];  
  23.                     Console.WriteLine("Name:" + bikeName);  
  24.                     Console.WriteLine("Total:" + issuedBike);  
  25.                     Console.WriteLine("InUse:" + inUseBike);  
  26.                     Console.WriteLine();  
  27.                 }  
  28.   
  29.             }  
  30.             Console.ReadLine();  
  31.         }  
My text file contains: 
Users of Yamaha: (Total of 2 bike issued; Total of 2 bike in use). line1....................something
line 2..................something
Bike details //emptyline
1125 Monica YamahaR15(v3.0)(petrol / 7788 4903), start Wed 1 / 18 8: 53 1128
Dhini Yamaha Fz(v2.0)(petrol / 7748 4903), start Wed 1 / 18 9: 53
 
 
 O/p like this...
  1. Name: Yamaha;  
  2.  Total: 1;  
  3.  Inuse: 1;  
  4.  members: Monica[Wed 1 / 18 8: 53], Dhini[Wed 1 / 18 9: 53] 
  I did something fine using my code but i can't achieve 100%.Please help me to solve the problem.
 
 

Answers (8)