Arpit Yadav

Arpit Yadav

  • NA
  • 1
  • 983

i am using this code for search details from text file

Apr 5 2014 7:02 AM
i am using this cod for search details from text file. when i am search program search from starting of the line. but i want that program must search from end point of line.
text line like ::= Date 02/02/2014#Time 12:00#Name Sonu

after the execution of code program display first date, time then name but i want program should search First NAME then time or date.....
Please give me sol.... .... thank you... 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace add_or__search_in_io
{
    class Program
    {
        struct details
        {
            public DateTime date;
            public string appointment;
            public string day;
            public string time;
        };
        private details testdetails;
        static FileStream f;
        StreamReader r;
        StreamWriter w;
        public void getdata()
        {
            string duedate;
            Console.WriteLine("Enter date in dd/mm/yyyy format");
            duedate = Console.ReadLine();
            if ((duedate.Length) != 10)
            {
                Console.WriteLine("Incorrect format");
                Console.ReadLine();
                Environment.Exit(0);
            }
            if ((duedate.Substring(2, 1) != "/") || (duedate.Substring(5, 1) != "/"))
            {
                Console.WriteLine("Invalid format");
                Console.ReadLine();
                Environment.Exit(0);
            }
           /* try
            {
                testdetails.date = Convert.ToDateTime(duedate);
                Environment.Exit(0);
            }
            catch
            {
                Console.WriteLine("Invalid date");
                Environment.Exit(0);
            }*/
            testdetails.date = Convert.ToDateTime(duedate);
            testdetails.day = Convert.ToString(testdetails.date.DayOfWeek);
            Console.WriteLine("Appointment with ");
            testdetails.appointment = Console.ReadLine();
            Console.WriteLine("Appointment time");
            testdetails.time = Console.ReadLine();
            if (testdetails.time.Length != 5)
            {
                Console.WriteLine("Incorrect format of time");
               // Environment.Exit(0);
            }
            if (testdetails.time.Substring(2, 1) != ":")
            {
          
                Console.WriteLine("Incorrect format");
               // Environment.Exit(0);
            }
            f = new FileStream("mytext.txt", FileMode.Append, FileAccess.Write);
            w = new StreamWriter(f);
            w.Write("Date : ");
            w.Write(testdetails.date.ToShortDateString());
            w.Write("?");
            w.Write("Day :");
            w.Write(testdetails.day);
            w.Write("?");
            w.Write("Name :");
            w.Write(testdetails.appointment);
            w.Write("?");
            w.Write("Time :");
            w.Write(testdetails.time);
            w.WriteLine("?");
            w.Flush();
            w.Close();
        }
        public void display()
        {
            string str;
            f = new FileStream("mytext.txt", FileMode.Open,FileAccess.Read);
            r = new StreamReader(f);
            Console.Clear();
            int pos = 0;
            while ((str = r.ReadLine()) != null)
            {
                while (true)
                {
                    pos = str.IndexOf("?");
                    if (pos == -1)
                        break;
                    Console.WriteLine(str.Substring(0,pos));
                    str = str.Substring(pos + 1);
                     }
                pos = 0;
            }
            r.Close();
        }
        public void search()
        {
            string str, chkstr1, chkstr2;
            DateTime dd; int pos;
            f = new FileStream("mytext.txt",FileMode.Open, FileAccess.Read);
            r = new StreamReader(f);
            Console.WriteLine("Ente date in dd/mm/yy format for search");
            dd = Convert.ToDateTime(Console.ReadLine());
            while ((str = r.ReadLine()) != null)
            {
                chkstr1 = "Date : " + dd.ToShortDateString();
                pos = str.IndexOf("?");
                chkstr2 = str.Substring(0, pos);
                if (chkstr1.CompareTo(chkstr2) == 0)
                {
                    while (true)
                    {
                        pos = str.IndexOf("?");
 
                        if (pos == -1)
                            break;
                        Console.WriteLine(str.Substring(0, pos));
                        str = str.Substring(pos + 1);
                    }
                    pos = 0;
                }
            }
            r.Close();
        }
        static void Main(string[] args)
        {
            Program p = new Program();
            int ch = 0;
            Console.Clear();
            while (ch != 4)
            {
                Console.WriteLine("1, Add appointment");
                Console.WriteLine("2, View appointment");
                Console.WriteLine("3, search");
                Console.WriteLine("4, exit");
                Console.WriteLine("5, enter choice");
                ch = Convert.ToInt32(Console.ReadLine());
                switch(ch)
                {
                    case 1:
                        {
                            Console.Clear(); p.getdata(); Console.Clear();
                        }break;
                    case 2:
                        {
                            Console.Clear(); p.display(); Console.ReadLine(); Console.Clear();
                        }break;
                    case 3:
                        {
                            Console.Clear(); p.search(); Console.ReadLine(); Console.Clear();
                        }break;
                    case 4:
                        {
                            return;
                        }
                    default:
                        {


Answers (1)