Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#4.0
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.....
 
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:
                        {
Please give me sol.... .... thank you...
Posted 23-Mar-14 7:38am
Edited 26-Mar-14 18:30pm
v3
Comments
CHill60 at 23-Mar-14 14:01pm
   
"immediately" ... how rude! I suggest that you use the Improve question link to remove that word.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 mhegazy94 460
1 Sergey Alexandrovich Kryukov 430
2 Ravi Bhavnani 190
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,135
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,619
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web04 | 2.8.140922.1 | Last Updated 27 Mar 2014
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100