i want to know how to read file and write on it in the same time for example:
file content:
Johny
tony
jack
Ahmad
Johny
string line;
line = file.ReadLine();
if (line == "johny")
{
line= "Sam"
}
i want to know how to read file and write on it in the same time for example:
Johny
tony
jack
Ahmad
Johny
string line;
line = file.ReadLine();
if (line == "johny")
{
line= "Sam"
}
You have to bear in mind that "Sam" and "Johnny" are not the same length. What will the file do with those empty bytes? Worse, what if you replaced "Sam" with "Johnny"? You will overwrite letters in the next record.
Fixed-width records can address this, but for a small file system, I would just read all into a list, then rewrite the whole list to file again. Or a different approach would be to set it all up in a database and let the database handle the reads and writes and you handle the business logic.
But just writing new data to a file on the fly as you are reading it is probably going to be more trouble than it's worth.