By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,870 Members | 1,212 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,870 IT Pros & Developers. It's quick & easy.

Writing to a spesific line

P: n/a
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
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #1
Share this Question
Share on Google+
6 Replies


P: n/a
Dakkar wrote:
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.

--
Hans Kesting
Nov 16 '05 #2

P: n/a
Dakkar wrote:
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.

--
Hans Kesting
Nov 16 '05 #3

P: n/a
if i want to change the word which is always at the bottom of the
text
is that possible?
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #4

P: n/a
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...

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
Nov 16 '05 #5

P: n/a
Thanks
*---------------------------------*
Posted at: http://www.GroupSrv.com
*---------------------------------*

Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 16 '05 #6

P: n/a
Magnus Krisell wrote:
"Hans Kesting" <ne***********@spamgourmet.com> wrote in message
news:eY**************@TK2MSFTNGP14.phx.gbl...
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.

--
Hans Kesting
Nov 16 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.