Writing to a spesific line

  • Thread starter Thread starter Dakkar
  • Start date Start date
D

Dakkar

I have a txt file like this



ClientFirstLaunch=no
NextLoginKey=255
LastServerID=1
PlayerName=Dakkar
DefaultChar=0
LastTip=3
ShowTips=no
Password=
SavePassword=on
AcctID=dakkar


and i want when my program executed it will change the savepassword
line to off
how can i do that?
Thanks
 
Dakkar said:
I have a txt file like this



ClientFirstLaunch=no
NextLoginKey=255
LastServerID=1
PlayerName=Dakkar
DefaultChar=0
LastTip=3
ShowTips=no
Password=
SavePassword=on
AcctID=dakkar


and i want when my program executed it will change the savepassword
line to off
how can i do that?
Thanks

I think the only way is:
- read in the entire file into some structure (string[] ?)
- find the line you want to change and change it
- rewrite the entire file with the changed content

You can't just change a single word in the file on disk.

If you have complete control over this text file, maybe you should
consider changing to Xml. You still need to read in (and write back)
the entire file, but finding the correct entry to change
is easier.
 
if i want to change the word which is always at the bottom of the
text
is that possible?
 
Hans Kesting said:
I think the only way is:
- read in the entire file into some structure (string[] ?)
- find the line you want to change and change it
- rewrite the entire file with the changed content

Not the only way. It's not neceassary to read the entire file into
memory (which is probably ok here, but the file might be huge).
I would use a temporary file and write something along the lines of:

using System.IO;

string inFilename = @"C:\testfile.txt";
string outFilename = inFilename + "_temp";

using (StreamReader reader = new StreamReader(inFilename))
{
using (StreamWriter writer = new StreamWriter(outFilename))
{
try
{
while (true)
{
string line = reader.ReadLine();
if (line == null) break;
string[] parts = line.Split('=');
if (parts.Length > 0 && parts[0] == "SavePassword")
{
writer.WriteLine("SavePassword=off");
}
else
{
writer.WriteLine(line);
}
}
}
catch (IOException)
{
// Something went wrong; delete the incomplete file
File.Delete(outFilename);
throw;
}
}
}
// Delete the original file and rename the new one
File.Delete(inFilename);
File.Move(outFilename, inFilename);

- Magnus
 
Magnus said:
I think the only way is:
- read in the entire file into some structure (string[] ?)
- find the line you want to change and change it
- rewrite the entire file with the changed content


Not the only way. It's not neceassary to read the entire file into
memory (which is probably ok here, but the file might be huge).
I would use a temporary file and write something along the lines of:

using System.IO;

string inFilename = @"C:\testfile.txt";
string outFilename = inFilename + "_temp";

using (StreamReader reader = new StreamReader(inFilename))
{
using (StreamWriter writer = new StreamWriter(outFilename))
{
try
{
while (true)
{
string line = reader.ReadLine();
if (line == null) break;
string[] parts = line.Split('=');
if (parts.Length > 0 && parts[0] == "SavePassword")
{
writer.WriteLine("SavePassword=off");
}
else
{
writer.WriteLine(line);
}
}
}
catch (IOException)
{
// Something went wrong; delete the incomplete file
File.Delete(outFilename);
throw;
}
}
}
// Delete the original file and rename the new one
File.Delete(inFilename);
File.Move(outFilename, inFilename);

- Magnus

You are right. This might be better, especially for large files.
The main point was: "you can't change something in the middle of a
textfile". You need to rewrite the entire file, using memory
or a temporary file to store the partially processed text.
 
Back
Top