Click here to Skip to main content
15,891,689 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Hospital automation code and at the delete section i need to change a specific information about the Patient without delete the whole line.

What I have tried:

i tried :
var file = new List<string>(System.IO.File.ReadAllLines("Hasta.txt")); //Aranan Tc'ye göre diziler kullanarak Hasta dosya'sında bilgileri sil file.RemoveAt(i); File.WriteAllLines("Hasta.txt", file.ToArray());
Posted
Updated 5-Feb-17 23:53pm
v2
Comments
Richard MacCutchan 6-Feb-17 4:30am    
What is the problem?
Also, why are you using text files for such a project? Some sort of database would make much more sense.

Text files don't have "lines" - they have "line terminator characters" which allow you to treat them as separate lines under some circumstances. But that doesn't mean that "lines are lines" - if your new data is one character bigger or smaller than the old data (and it will be) then you can't just "re-write a line", as you have to insert or delete character form the entire rest of the file to do that.

Text files are not a good choice here - the only safe way to update them is the read the file up to the record you want to change, writing it to a new file as you go, then write the new information, skip the old line, and read the rest of the file, writing that to the new file. When you are finished, close both files, delete the original, and rename the new.

The other alternative is to read all the text - as you are - then change your record and write it all back as a new file. Then delete the old and rename the new.

But ... hospitals are never single user environments so this is fraught with danger and has a high risk of losing data as a result. All it take is two users trying to change different records at the same time, and somebody is going to lose what they entered!

Instead, consider using a database - Sql Server or MySql are good choices - and let it handle the files themselves, while you just tell it what to insert, what to update, and so on - it'll also be a lot, lot faster when you come to run the code!
 
Share this answer
 
Comments
Member 12897180 6-Feb-17 6:10am    
actually i used the alternative way that you said and it worked but the problem i had their is .... that the remove command is deleting all the patient information not just the id number or the name or the family name ... how can i remove the object that just i want to change .
thanks for helping :)
OriginalGriff 6-Feb-17 7:24am    
No, you aren't.
You are appending the "updated" info to the end of the file, not changing the existing data.
You read the file into "file", remove a line, then add a new row at the end before writing it all back.
So use the debugger, and look at what is in hastaBilgileri just before you write it.
Member 12897180 6-Feb-17 7:54am    
OKEY i'll try it
Member 12897180 12-Feb-17 1:36am    
sir i still have the same problem which i couldn't reach for just one object from the whole line i won't to rewrite the whole line just for updating one information .... i split them here :
string[] satirdakiHastaBilgileri = satirlar[i].Split(';');

and write them here :

hastaBilgileri = hastaTc + ";" + hastaAdi + ";" + hastaSoyadi + ";" + hastaDogumTarih + ";" + hastaTel + ";" + hastaKayitNo + ";" + hastaCinsiyet;

yazici.WriteLine(hastaBilgileri);


at this commands i just can split every row which is (hastaBilgileri) but i couldn't split every information which is (hastaTC) or (hastaAdi) or (hastaSoyadi) or ......
the question here is how could i split every information ?
thanks for helping :)
Member 12897180 6-Feb-17 6:11am    
if (secim == 2)
{
Console.WriteLine("Aranan Hastaya ait TcKimlik Noyu Giriniz");
arananTc = Console.ReadLine();
Console.WriteLine("_________________________________________________________");
Console.WriteLine("TC KİMLİK\tADI\tSOYADI\tYAŞ\tTELEFON\tKayit NO\tCİNSİYET");
//Hasta dosyasındakı bilgileri oku
string[] satirlar = File.ReadAllLines("Hasta.txt");

//hasta dosyasındaki lineler teker teker oku va ayır
for (int i = 0; i < satirlar.Length; i++)
{
string[] satirdakiHastaBilgileri = satirlar[i].Split(';');


if (satirdakiHastaBilgileri[0] == arananTc)
{

Console.Write(satirdakiHastaBilgileri[0] + "\t" + satirdakiHastaBilgileri[1] + "\t" + satirdakiHastaBilgileri[2] + "\t" + satirdakiHastaBilgileri[3] + "\t" + satirdakiHastaBilgileri[4] + "\t" + satirdakiHastaBilgileri[5] + "\t" + satirdakiHastaBilgileri[6]);

var file = new List<string>(System.IO.File.ReadAllLines("Hasta.txt"));
//Aranan Tc'ye göre diziler kullanarak Hasta dosya'sında bilgileri sil
file.RemoveAt(i);
File.WriteAllLines("Hasta.txt", file.ToArray());


// yeni dosya oluştur
StreamWriter yazici = File.AppendText("Hasta.txt");

//ilk önce bilgileri yaz ondan sonra koşula geç

Console.WriteLine( );
// Oluşturulan dosya'nın içine istediğin bilgileri oluştur
Console.Write("Hastanin yeni TC Kimlik Nosunu giriniz:");
hastaTc = Console.ReadLine();
Console.Write("Hastanin yeni Adini giriniz:");
hastaAdi = Console.ReadLine();
Console.Write("Hastanin yeni Soyadini giriniz:");
hastaSoyadi = Console.ReadLine();
Console.Write("Hastanin yeni Yaşini giriniz:");
hastaDogumTarih = Convert.ToInt32(Console.ReadLine());
Console.Write("Hastanin yeni Telefonunu giriniz:");
hastaTel = Console.ReadLine();
Console.Write("Hastanin yeni Kaydin NO ");
hastaKayitNo = Convert.ToChar(Console.ReadLine());
Console.Write("Hastanin yeni Cinsiyetini giriniz:");
hastaCinsiyet = Convert.ToChar(Console.ReadLine());

hastaBilgileri = hastaTc + ";" + hastaAdi + ";" + hastaSoyadi + ";" + hastaDogumTarih + ";" + hastaTel + ";" + hastaKayitNo + ";" + hastaCinsiyet;

yazici.WriteLine(hastaBilgileri);
Console.WriteLine();
yazici.Close();
Console.WriteLine("Kayit Başariyla eklendi. :) ");
Console.ReadLine();

}
Console.ReadLine();

}
}
Without seeing the content of text file, i'd suggest to start with basics:
How to: Read Text from a File[^]
How to: Write Text to a File[^]

But i have a feeling that you need to use XmlSerialization[^]
 
Share this answer
 
Comments
Member 12897180 6-Feb-17 6:16am    
thanks sir i actually read this sites but i didn't understand what XmlSerialization is ?
if you read the comment in the solution 1 you will know what i needed ..

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900