0

I am doing an appointment program wherein I am adding few appointments based on date and stored in a text file. I have a problem in displaying the appointment details according to the date entered.The stored appointments in a text file looks like this..

Date & Time :08/08/2013 09:30 AM Person Name :Shiv

Date & Time :08/08/2013 10:30 AM Person Name :Sanjay

Date & Time :10/08/2013 09:30 PM Person Name :Kumar

Problem is when I enter any date to search appointments,suppose there are 2 appointments for that particular date, my output is showing only one appointment.

Example: If I enter date 08/08/2013,There are 2 appointments stored in text file for the entered date but my output is showing only one appointment like this

Appointment Details

08/08/2013 09:30 AM Person Name :Shiv

My code:

Console.WriteLine("Enter the date to search appointment in (dd/mm/yyyy) format");
string Date = Console.ReadLine();

string str = sr.ReadToEnd();

bool isDate = File.ReadAllText("E:\\Practice/C#/MySchedular.txt").Contains(Date) ? true : false;

if (isDate)        
{             
    string searchWithinThis = str; ;

    int CharacterPos = searchWithinThis.IndexOf(Date);      

    sr.BaseStream.Seek(CharacterPos ,SeekOrigin.Begin);

    str = sr.ReadLine();

    Console.WriteLine("\n*********Appointment Details*********");

    Console.WriteLine("{0}", str);

    Console.WriteLine("\n");    
}            
else        
{           
    Console.WriteLine("No appointment details found for the entered date");        
}
4

1 に答える 1

0

おそらく、次のようなものが機能します。

static void Main(string[] args)
{
    string filename = @"E:\Practice\C#\MySchedular.txt";
    string fileContent;
    Console.WriteLine("Enter the date to search appointment in (dd/mm/yyyy) format");
    string date = Console.ReadLine();

    using (StreamReader sr = new StreamReader(filename))
    {
        fileContent = sr.ReadToEnd();
    }

    if (fileContent.Contains(date))
    {
        string[] apts = fileContent.Split('\n').Where(x => x.Contains(date)).ToArray();

        foreach (string apt in apts)
        {
            Console.WriteLine("\n**Appointment Details**");
            Console.WriteLine("{0}", apt);
            Console.WriteLine("\n");
        }
    }
    else
    {
        Console.WriteLine("No appointment details found for the entered date");
    }
    Console.Read();
}
于 2013-10-05T18:21:53.800 に答える