[C#]Porównanie danych w pliku tekstowym

0

Mam taki oto kod. I teraz mam porównać czy tekst wpisywany przez użytkownika znajduje się już w pliku tekstowym, jak to zrobić?

                          using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ConsoleApplication2
{
    class Program
    {
            static void Main(string[] args)
            {
                Console.WriteLine("Czytam Dane....\n");
                            
                            string[] data = File.ReadAllLines("data.txt");
                            List<Persons> output = new List<Persons>();
                            char[] delimiters = { ',' };

                            foreach (string line in data)
                            {
                                Persons tmp = new Persons();
                                string[] parsedLine = line.Split(delimiters);
                                tmp.Name = parsedLine[0];
                                tmp.Surname = parsedLine[1];
                                int.TryParse(parsedLine[2], out tmp.Age);

                                output.Add(tmp);
                                
                            }

                            foreach (Persons singlePerson in output)
                            {
                                Console.Write(singlePerson.ToString());
                                Console.WriteLine("---------");
                            }

                string tn;            
                Console.WriteLine("Czy chcesz cos dopisac? (t/n)\n");
                tn = Console.ReadLine();
                if (tn == "t")
                {
                    string name, surname, calosc;
                    int age;

                    Console.WriteLine("Wpisz imie\n");
                    name = Console.ReadLine();
                    Console.WriteLine("Wpisz nazwisko\n");
                    surname = Console.ReadLine();
                    Console.WriteLine("Wpisz wiek\n");
                    age = Console.Read();

                    calosc = "" + name + "," + surname + "," + age.ToString() + ","; // Obejście głupiego błędu          
                    
                    FileStream file = new FileStream("data.txt", FileMode.Append, FileAccess.Write);

                    StreamWriter sw = new StreamWriter(file);
                    sw.WriteLine(calosc);
                    sw.Close();



                }
                else
                {
                }



                
        }
        
       public class Persons
        {
            public string Name;
            public string Surname;
            public int Age;

            public Persons() : this("N/A", "N/A", -1) { }
            public Persons(string name, string surname, int age)
            {
                this.Name = name;
                this.Surname = surname;
                this.Age = age;
            }

           public override string ToString()
            {
                StringBuilder sb = new StringBuilder();
                sb.AppendLine("Osoba :");
                sb.AppendLine("Imie: " + this.Name);
                sb.AppendLine("Nazwisko: " + this.Surname);
                sb.AppendLine("Wiek: " + this.Age.ToString());
                
                return sb.ToString();
            }
        }
    }
}
0

Nie prościej zamiast:

                     
calosc = "" + name + "," + surname + "," + age.ToString() + ","; // Obejście głupiego błędu          
                
FileStream file = new FileStream("data.txt", FileMode.Append, FileAccess.Write);

StreamWriter sw = new StreamWriter(file);
sw.WriteLine(calosc);
sw.Close();

Dać:

string path = @"C:\temp\data.txt";
...
calosc = "\r\n" + name + "," + surname + "," + age + ","; // to nie jest C++, int się sam zamieni w string ;)
File.AppendAllText(path, calosc); 
Dan900 napisał(a)

Mam taki oto kod. I teraz mam porównać czy tekst wpisywany przez użytkownika znajduje się już w pliku tekstowym, jak to zrobić?

Odczytane Persony masz w liście "output". Więc przeszukaj sobie tę kolekcję w poszukiwaniu określonej persony. Możesz w tym celu np. przeciążyć metodę Equals() w klasie Person, a później użyć output.Contains(Person p), i jeśli zwróci true, to nie zapisujesz.
A jeśli używasz .NET 3.5, to wystarczy:

if (!output.Any(x => x.Name == name && x.Surname == surname))
    File.AppendAllText(path, calosc);
else
    Console.WriteLine("Taka osoba już istnieje!");

P.S. Nie robi się pól z modyfikatorem dostępu "public".

0

Dziękuję za pomysł, działa.

1 użytkowników online, w tym zalogowanych: 0, gości: 1