473,382 Members | 1,353 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,382 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 1530
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>
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.