The online racing simulator
[RANKING&SCORING] Need help with create/delete things inside a file.
Hello Programmers,

Im working on a Open Source scoring&ranking system and needs help with creating/deleting a text line as example INSIDE a file.

Ive allready got the code for creating an file:
Its this one:

case "!setadmin":
if (StrMsg.Length > 1)
{
if (Connections[GetConnIdx(MSO.UCID)].IsAdmin == 1)
{
if (Connections[GetConnIdx(MSO.UCID)].InsimAdmin == 0)
{
StreamReader ApR = new StreamReader(UserBase + "\\groups\\Admin.txt");
string TempAPR = ApR.ReadToEnd();
ApR.Close();
StreamWriter Ap = new StreamWriter(Base + "\\groups\\Admin.txt");
Ap.WriteLine(TempAPR + "Admin = " + Msg.Remove(0, 10));
Ap.Flush();
Ap.Close();

foreach (clsConnection C in Connections)
{
if (C.Username == Msg.Remove(0, 10))
{
C.Admin = 1;
InSim.Send_MST_Message("/msg ^7 " + C.PlayerName + "^7 got Admin rights!");
}
}
}
else
{
InSim.Send_MTC_MessageToConnection("^1Warning:^7 This user is already a Admin!", MSO.UCID, 0);
}
}
}
else
{
InSim.Send_MTC_MessageToConnection("^1Command Error. Please try again", MSO.UCID, 0);
}
break;

===========================================
My Setups:

Language: C#
Database: txt (soon sql)
Microsoft Visual C# 2008 Express Version SP1
Libary: LFS_External 1.1.1.4

===========================================

I will be very happy if someone can help me.
And the Community will be happy to if i can fish my open source Ranking&Scoring system

Regards Grinch
-
(MariusMM) DELETED by MariusMM
Hey,

thanks MariusMM but ive allready written that ive got the create code for the file. but no problem

maybe u sadly dont understand my problem so i explain it:

with this code above you and i have posted we can create a file like this:
_____________________________
|Filename.txt
|--------------------------------|
| Admin = Grinch
| Admin = MariusMM
| Adm....
|
|____________________________|

But i need a code wich i can delete this things, ill explain that too:

if i type as example "!lockadmin [username]", that its deleting the users Admin "rights" and it should delete the users: "Admin = username".
So the code wich i search should to the opposite
Thats what i mean

Did you or anybody got any clue
maybe dougie-lampkin personaliy? ^^ cuz he has written the code

Regards Grinch
#3 - amp88
So you want to take a specific input (in this case the driver's name) and remove it from the "Filename.txt" file if it appears there?

What I would normally do in this case is to create a secondary file and copy all the contents of the "Filename.txt" file into that secondary file EXCEPT for the line you want to delete. Then when you've copied the file you delete "Filename.txt" and rename your secondary file "Filename.txt.temp" or whatever you call it to "Filename.txt". There are other ways to do it (like reading all of the contents of "Filename.txt" EXCEPT for the line you want to delete into a Vector or some temporary data structure in memory then just creating a new file by outputting all the data from memory, but I think the first way is probably the easiest.

Here is some code from my custom resolution launcher for LFS that demonstrates the first method (but rather than removing the line entirely I'm just modifying some of it). It's in Java, so you might not understand all the syntax but hopefully it gives you some inspiration. Good luck.

private void outputDetailsToCFGFile() {
/*
* Output the details the user has specified to file. Create a temp file and
* to hold the contents of the updated cfg.txt file then delete the original
* cfg.txt file and rename the temp file to cfg.txt
*/
// The original config file
File configFile = new File(CONFIG_FILENAME);
FileReader configFileReader = null;
BufferedReader bufReader = null;

try {
configFileReader = new FileReader(configFile);
bufReader = new BufferedReader(configFileReader);
} catch(FileNotFoundException fnfe) {
// Can't find the cfg.txt file, exit the application
JOptionPane.showMessageDialog(null, "The "+CONFIG_FILENAME+" file cannot be found at the following location:\n" +
configFile.getAbsolutePath()+"\n" +
"Try running LFS on its own to create the "+CONFIG_FILENAME+" file then re-run this application.",
"Cannot Find Config File",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}

// The temporary config file
File newConfigFile = new File("temporary"+CONFIG_FILENAME);

FileOutputStream newConfigOutputStream = null;
PrintWriter writer = null;

try {
newConfigOutputStream = new FileOutputStream(newConfigFile);
writer = new PrintWriter(newConfigOutputStream);
} catch(FileNotFoundException fnfe) {
fnfe.printStackTrace();

JOptionPane.showMessageDialog(null, "The application cannot write the details of the new config file.\n" +
"Please report this problem in the LFS Forum thread you downloaded the application from.\n" +
"Include as many details as possible (including Operating System, Java version, " +
"folder you're running this app from).\n" +
"Thanks.",
"Cannot Write New File",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}

/*
* Read files from the original config file. If they are not relevant to this application simply
* output them to the new config file. If they are relevant update the information as necessary
* and output it
*/

try {
String inputLine = bufReader.readLine();

while(inputLine != null) {
if(inputLine.startsWith(SCREEN_MODE_LINE_IDENTIFIER)) {
if(debug) {
print("Found the screen info line in config file: "+inputLine);
}

// If the colour depth isn't set here it needs to be retained from current file
if(colourDepth == Integer.MAX_VALUE) {
// Get the details in the current screen info
String currentDetails = inputLine.substring(inputLine.indexOf(SCREEN_MODE_LINE_IDENTIFIER)+
SCREEN_MODE_LINE_IDENTIFIER.length()+1);

String currentColourDepth = currentDetails.substring(0, 1);

StringBuilder toOutput = new StringBuilder(SCREEN_MODE_LINE_IDENTIFIER+" ");

// Colour depth
toOutput.append(currentColourDepth+" ");

// Refresh rate
toOutput.append(refreshRate+" ");

// Horizontal res
toOutput.append(horizontalRes+" ");

// Vertical res
toOutput.append(verticalRes+" ");

writer.println(toOutput.toString());
} else {
// Otherwise just output the new details
StringBuilder toOutput = new StringBuilder(SCREEN_MODE_LINE_IDENTIFIER+" ");

// Colour depth
if(colourDepth == 16) {
toOutput.append("1 ");
} else if(colourDepth == 32) {
toOutput.append("0 ");
}

// Refresh rate
toOutput.append(refreshRate+" ");

// Horizontal res
toOutput.append(horizontalRes+" ");

// Vertical res
toOutput.append(verticalRes+" ");

writer.println(toOutput.toString());
}
} else if(inputLine.startsWith(WINDOW_MODE_LINE_IDENTIFIER)) {
if(debug) {
print("Found the windowed mode line in config file: "+inputLine);
}

if(windowed == null) {
// No windowed preference has been specified, just output the current line
writer.println(inputLine);
} else {
if(windowed.booleanValue()) {
writer.println(WINDOW_MODE_LINE_IDENTIFIER+" 1");
} else {
writer.println(WINDOW_MODE_LINE_IDENTIFIER+" 0");
}
}
} else {
writer.println(inputLine);
}

inputLine = bufReader.readLine();
}

writer.close();
bufReader.close();
} catch(IOException ioe) {
ioe.printStackTrace();

JOptionPane.showMessageDialog(null, "The application cannot read or write the config file.\n" +
"Please report this problem in the LFS Forum thread you downloaded the application from.\n" +
"Include as many details as possible (including Operating System, Java version, " +
"folder you're running this app from).\n" +
"Thanks.",
"Problem Reading Or Writing Config File",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}

// Now the temporary config file contains the updated details delete the original file and rename new one
boolean originalFileDeleted = configFile.delete();

if(originalFileDeleted) {
boolean newFileRenamed = newConfigFile.renameTo(configFile);

if(!newFileRenamed) {
JOptionPane.showMessageDialog(null, "The application cannot replace the original config file (2).\n" +
"Please report this problem in the LFS Forum thread you downloaded the application from.\n" +
"Include as many details as possible (including Operating System, Java version, " +
"folder you're running this app from).\n" +
"Thanks.",
"Problem Replacing Config File",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}
} else {
JOptionPane.showMessageDialog(null, "The application cannot replace the original config file (1).\n" +
"Please report this problem in the LFS Forum thread you downloaded the application from.\n" +
"Include as many details as possible (including Operating System, Java version, " +
"folder you're running this app from).\n" +
"Thanks.",
"Problem Replacing Config File",
JOptionPane.ERROR_MESSAGE);

System.exit(1);
}
}

-
(MariusMM) DELETED by MariusMM
@ both:
You helped me alot now i know where to start

@ amp88:
Sadly i dont understand much Java, but ive understand the synax wich you wanted to show me.

@ MariusMM:
Hell,YEA! exactly what ive searched
i hope i can get it all to work

You are the best community wich i ever seen!

Regards Grinch
Oh damnit... i dont get it
but i want it ass hell...

Dont Dougie-Lampkin know's that how it works?
because he wrote the code for writing,reading a textfile in C#
#6 - amp88
Quote from Grinch :Oh damnit... i dont get it
but i want it ass hell...

Can you post the code you've currently got (even if it's not working at all or not working correctly)?
Its not working at all
im using the code from MariusMM.
but ive read in an C# forum that its not possible to delete some line in a textfile in C#
the only method is to save the complete text in a .temp file ,remove the line wich you dont need/want, save into the old file.

but i dont know how to do
-
(MariusMM) DELETED by MariusMM
#8 - amp88
Quote from Grinch :the only method is to save the complete text in a .temp file ,remove the line wich you dont need/want, save into the old file.

Yeah, that's what I was trying to demonstrate above. I don't have a C# IDE installed at the moment but what you want to do is to set up 2 different FileStreams. One in read mode (to read in your "Filename.txt" file) and one in write mode (to write out the temporary copy "Filename.txt.temp"). Then you read one line from the "Filename.txt" file into a string. Once you've read it in you want to look at that string and decide if you want to keep it or you want to get rid of it. If you want to keep it you write it out to your "Filename.txt.temp" file. If you want to get rid of it (because it contains the name you want to delete) you don't write it out to the "Filename.txt.temp" file. Do that for all of the lines in the "Filename.txt" file. Then once you're finished delete the "Filename.txt" file and rename the "Filename.txt.temp" file to "Filename.txt".
Quote from MariusMM :I told you it didn't work properly, that you had to play with it to work.

Ive tryed all wich i know and tryed other things to but no sucsess

@amp88:
yea thats exactly what ive searched and found but dont know how to use it in LFS_External

Im trying my best, but if you get some good news please post them here.

Regards Grinch
u said u wanna delete a .txt file ?
try this
private static Delete(string path);
File.Delete(@"C:MyDoc.txt");

i hope it helps
Regarts Heiko
Quote from Marco1 :u said u wanna delete a .txt file ?
try this
private static Delete(string path);
File.Delete(@"C:MyDoc.txt");

i hope it helps
Regarts Heiko

No, he doesn't just want to delete a file. He wants to remove a specific line from a file. Say the file contained the following:

Admin:amp88
Admin:Marco1
Admin:Grinch

He wants for an admin to type a command like "!removeadmin amp88" and for a function to go through the file looking for the line "Admin:amp88" and remove it from the file. That way the file would end up looking like this:

Admin:Marco1
Admin:Grinch

and why he doesnt create each admin a .txt ? maybe it wants be easyer
@Marco1
Thats it!!!!
Then i just need to delete the whole file

e.g

ive got this database just as example:

C:\.....\Database\Admins\Grinch.txt as Admin database
C:\......\Database\Users\Grinch.txt as Normal user database
and
C:\......\Database\Moderators\Grinch.txt as Moderators database


YEA HELL IVE GOT IT!
sorry for swearing ^^

Mega best regards to the best community of the world! Grinch
Edit: sorry didn't see the post above.

I'm lazy, but I would just read the file into memory, make my changes, then dump it back out.

string path = @"Path\To\My\File";
List<string> admins = new List<string>();

// Read file.
using (StreamReader reader = new StreamReader(path))
{
string line = null;
while ((line = reader.ReadLine()) != null)
{
string[] tokens = line.Split(':');
admins.Add(tokens[1]);
}
}

// Make changes to the admins.
admins.Remove("Grinch");
admins.Add("DarkTimes");

// Write file.
using (StreamWriter writer = new StreamWriter(path))
{
foreach (string admin in admins)
{
writer.WriteLine("Admin:{0}", admin);
}
}

Quote from DarkTimes :Edit: sorry didn't see the post above.

I'm lazy, but I would just read the file into memory, make my changes, then dump it back out.

That's what I do when I need to delete a simple line...

As a creator from an finished Ranking System, I really suggest you to drop that txt shit, and start with a MySQL database. I'm currently rewriting the system into C# and with a MySQL database. This database is a lot more easier to work with, although its syntax has some slight differences from the PHP Mysql, and it's enormously faster.

I hope I can help you with these links:
C connector: http://dev.mysql.com/downloads/connector/c/6.0.html
C++ connector: http://dev.mysql.com/downloads/connector/cpp/1.0.html
.Net connector: http://dev.mysql.com/downloads/connector/net/6.0.html
^ You should see what version of these connectors suits you the best, I just took the one that (almost) matched my MySQL version but it seems that the newer connectors also support lower MySQL versions.
A site with basic VB MySQL queries: http://www.linglom.com/2009/03 ... i-perform-sql-operations/

Good luck and if you need some question, there are a bunch of programmers willing to help you
Alternatively if you just want a database for your .NET app, nothing fancy, then consider SQL Compact. You can include a database with your app without any additional dependencies.
Thanks for ya help!

An other question:

Isn't a database wich is located on your own computer safer? as one wich is on an webserver?
And can i run an MySQL database on my home located computer?
Quote from Grinch :Thanks for ya help!

An other question:

Isn't a database wich is located on your own computer safer? as one wich is on an webserver?
And can i run an MySQL database on my home located computer?

Q1:
Yes, but then your computer has to be online 24/24 7/7. Well, if you use a database which is really located on your computer and not on the hosts computer. I would suggest using a web server because from there you can easily connect your ranking database to a PHP site which shows the current (and updated) rankings on the site.
Although an online MySQL database is very save too if you don't connect it to a site for example where users could enter ... how's it called again... MySQL injections.

Q2:
Yes, you can run a MySQL database from your own computer (from your localhost). You need a program for this. The program I use is called XAMPP and you can download it here: http://www.apachefriends.org/en/xampp-windows.html#641

It's a very user friendly tool if you want to setup a database.

Some quick information:
Install Xampp, open it, activate the MySQL part, open your browser and fill in the URL: localhost/phpmyadmin

It can get a bit new if you never done it before but the site I gave you earlier (about the MySQL queries from a .net application) will help you alot. If you have experience using PHP in combination with MySQL, you're one step ahead.
Wow!
Thank you very very much!

im on lfs since Feb 2007 but im only realy active in lfs and programming an InSim app since Apr 2009
Our Community ROCKZ!

Regards Grinch

FGED GREDG RDFGDR GSFDG