473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Writing to a spesific line

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
6 1531
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
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
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
"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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Dakkar | last post by:
I have a txt file like this ClientFirstLaunch=no NextLoginKey=255 LastServerID=1 PlayerName=Dakkar DefaultChar=0 LastTip=3
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
2
by: Craig | last post by:
I have the need to write a byte of information to a specific location in a text file. eg. the file looks something like this. FYYNN Line 1 Line 2 <eof>
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.