Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out? Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Go Back  Xtreme Visual Basic Talk > > > Reading a Textfile and sorting things out?


Reply
 
Thread Tools Display Modes
  #1  
Old 05-11-2009, 11:10 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default Reading a Textfile and sorting things out?


I have a txt file, that i want to read into my program. The problem is this:
The textfile is a stats file for a game and looks like this:

Code:
ip:port wins username points
ip:port wins username points
ip:port wins username points
ip:port wins username points
ip:port wins username points
etc.
The main thing i want to do is sort these things out and then add to mysql database table that would have these columns:

Code:
ip authid wins username points
So what i wanna do is splitting this textfile data up and then adding everything to a mysql table.

So, would this be possible?
Reply With Quote
  #2  
Old 05-11-2009, 05:11 PM
Gruff's Avatar
GruffReading a Textfile and sorting things out? Gruff is offline
Bald Mountain Survivor

Retired Moderator
* Expert *
 
Join Date: Aug 2003
Location: Oregon, USA - deceased
Posts: 6,440
Default

Sure.
__________________
Burn the land and boil the sea
You can't take the sky from me


~T
Reply With Quote
  #3  
Old 05-12-2009, 06:01 AM
SaHell SaHell is offline
Freshman
 
Join Date: Apr 2009
Location: Holland
Posts: 29
Default

You might find this useful and check the String.Split Method
__________________
Perfection is my dearest enemy ... Failure my hated friend ...
Reply With Quote
  #4  
Old 05-12-2009, 06:01 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

So, Do you have any tip or code i Can start from?
Reply With Quote
  #5  
Old 05-12-2009, 06:58 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

I suppose you would want to start with reading the file. See HERE to start and then SaHell's links should support the other things you need to do.

Have a go with it and if you get any specific problems, let us know.
__________________
Chris
Reply With Quote
  #6  
Old 05-19-2009, 12:13 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Hmm, this is the code which i am trying to rewrite in visual basic, i think it's in c++, but i am not shure:
Code:
#include <amxmodx>
#include <sqlx>

#define VERSION "1.04"

/***************************************
********* QUERIES
***************************************/

#define QUERY_CREATE_TABLE "CREATE TABLE IF NOT EXISTS `%s` ( \
				`authid` VARCHAR( 24 ) NOT NULL, \
				`wins` INT( 6 ) NOT NULL, \
				`lastname` VARCHAR( 32 ) NOT NULL, \
				`timestamp` INT( 12 ) NOT NULL, \ 
				`points` INT( 8 ) NOT NULL, \
				`serverip` VARCHAR( 21 ) NOT NULL, \
				PRIMARY KEY ( `authid` ) );"

#define QUERY_CLEAR_TABLE "DELETE FROM `%s` WHERE `serverip` = '%s';"
	
#define QUERY_FIX_TABLE "REPAIR TABLE `%s`; OPTIMIZE TABLE `%s`;"

#define QUERY_INSERT_DATA "INSERT INTO `%s` ( `authid`, `wins`, `lastname`, `timestamp`, `points`, `serverip` ) \
						VALUES ( '%s', '%i', '%s', '%i', '%i', '%s' );"
						
#define QUERY_INSERT_UPDATEFLAG "INSERT INTO `%s` ( `authid`, `wins`, `lastname`, `timestamp`, `points`, `serverip` ) \
						VALUES ( 'updating', '0', '', '%i', '0', '%s' );"

#define QUERY_DELETE_UPDATEFLAG "DELETE FROM `%s` WHERE `authid` = 'updating' AND serverip = '%s';"

/***************************************
********* VARIABLES
***************************************/

// globals
new cvHost, cvUser, cvPass, cvDB, cvTable, cvRate, init, table[32], insert_error, serverip[22], queryNum, queryIter;

// mysql
new Handle:tuple, Handle:db, Handle:query, mkQuery[512], error[1024], errcode;

/***************************************
********* PLUGIN LOAD AND UNLOAD
***************************************/

public plugin_init()
{
	register_plugin("GunGameSQL",VERSION,"Avalanche");
	register_cvar("gg_sql_version",VERSION,FCVAR_SERVER);
	set_cvar_string("gg_sql_version",VERSION);
	
	get_user_ip(0,serverip,21,0);

	cvHost	= register_cvar("gg_sql_host","127.0.0.1",FCVAR_PROTECTED);
	cvUser	= register_cvar("gg_sql_user","root",FCVAR_PROTECTED);
	cvPass	= register_cvar("gg_sql_pass","",FCVAR_PROTECTED);
	cvDB		= register_cvar("gg_sql_db","amx",FCVAR_PROTECTED);
	cvTable	= register_cvar("gg_sql_table","gg_stats",FCVAR_PROTECTED);

	cvRate	= register_cvar("gg_sql_updaterate","3");

	set_task(1.0,"sql_init");
	set_task(2.0,"count_map_load");
}

public plugin_end()
{
	sql_uninit();
}

/***************************************
********* MAIN FUNCTIONS
***************************************/

// keep track of map loads and only update database every so often
public count_map_load()
{
	// could not establish connection
	if(!init) return;

	// get how many plugin loads more until we prune
	new buffer[8], sql_update;
	get_localinfo("gg_sql_update",buffer,7);
	sql_update = str_to_num(buffer);

	// localinfo not set yet
	if(sql_update == 0)
	{
		// set to rate minus one
		get_pcvar_string(cvRate,buffer,7);
		num_to_str(str_to_num(buffer)-1,buffer,7);
		set_localinfo("gg_sql_update",buffer);

		return;
	}

	// time to update
	else if(sql_update <= 1)
	{
		// reset our count
		get_pcvar_string(cvRate,buffer,7);
		set_localinfo("gg_sql_update",buffer);

		update_database();

		return;
	}

	// decrement our count
	num_to_str(sql_update-1,buffer,7);
	set_localinfo("gg_sql_update",buffer);
	
	// tune-up
	get_pcvar_string(cvTable,table,31);
	format(mkQuery,511,QUERY_FIX_TABLE,table,table);
	SQL_ThreadQuery(tuple,"sql_query_handler",mkQuery);
}

// transfer stats file to website
public update_database()
{
	new sfFile[64], sfLineData[81], sfAuthid[48], sfWins[6], sfName[64], sfTimestamp[12], sfPoints[8];

	get_cvar_string("gg_stats_file",sfFile,63);
	trim(sfFile);

	// stats disabled/file doesn't exist
	if(!sfFile[0] || !file_exists(sfFile))
	{
		log_amx("Could not update database: stats file ^"%s^" does not exist",sfFile);
		return;
	}

	// clear old entries
	get_pcvar_string(cvTable,table,31);
	format(mkQuery,511,QUERY_CLEAR_TABLE,table,serverip);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0])
	{
		log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);
		SQL_FreeHandle(query);
		return;
	}
	SQL_FreeHandle(query);
 	 
	// insert update flag
	formatex(mkQuery,511,QUERY_INSERT_UPDATEFLAG,table,get_systime(),serverip);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0])
	{
		log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);
		SQL_FreeHandle(query);
		return;
	}
	SQL_FreeHandle(query);

	new lines, file = fopen(sfFile,"rt");
	if(!file) return;
	
	queryIter = 0;
	queryNum = 0;

	while(!feof(file))
	{
		// one of our threaded queries produced an error
		if(insert_error)
		{
			fclose(file);
			return;
		}

		lines++;
		fgets(file,sfLineData,80);

		// isolate authid
		strtok(sfLineData,sfAuthid,23,sfLineData,80,'^t');

		// isolate wins
		strtok(sfLineData,sfWins,5,sfLineData,80,'^t');

		// isolate name
		strtok(sfLineData,sfName,31,sfLineData,80,'^t');

		// isolate timestamp (and points)
		strtok(sfLineData,sfTimestamp,11,sfPoints,7,'^t');

		// clean to prevent SQL injection
		replace_all(sfAuthid,47,"'","\'");
		replace_all(sfName,63,"'","\'");

		// put it in the database
		queryNum++;
		formatex(mkQuery,511,QUERY_INSERT_DATA,table,sfAuthid,str_to_num(sfWins),sfName,str_to_num(sfTimestamp),str_to_num(sfPoints),serverip);
		SQL_ThreadQuery(tuple,"sql_query_handler",mkQuery);
	}

	fclose(file);

	if(!insert_error) log_amx("Updated the database with %i entries",lines);
}

/***************************************
********* SQL FUNCTIONS
***************************************/

// opens the database
public sql_init()
{
	init = 0;

	new host[32], user[32], pass[32], dbname[32];
	get_pcvar_string(cvHost,host,31);
	get_pcvar_string(cvUser,user,31);
	get_pcvar_string(cvPass,pass,31);
	get_pcvar_string(cvDB,dbname,31);

	tuple = SQL_MakeDbTuple(host,user,pass,dbname);
	if(tuple) db = SQL_Connect(tuple,errcode,error,1023);

	if(db == Empty_Handle)
	{
		log_amx("Could not connect to database. Error #%i: %s",errcode,error);
		return 0;
	}

	get_pcvar_string(cvTable,table,31);
	format(mkQuery,511,QUERY_CREATE_TABLE,table);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0])
	{
		log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);
		SQL_FreeHandle(query);
		return 0;
	}
	SQL_FreeHandle(query);

	init = 1;
	return init;
}

// closes the database
public sql_uninit()
{
	if(init)
	{
		if(db) SQL_FreeHandle(db);
		if(tuple) SQL_FreeHandle(tuple);
	}
}

// checks the query for failure
public sql_query_handler(failstate,Handle:query,error[],errnum,data[],size,Float:queuetime)
{
	// report first error that we come across
	if(failstate != TQUERY_SUCCESS && !insert_error)
	{
		insert_error = 1;

		SQL_GetQueryString(query,mkQuery,511);
		log_amx("Could not execute query ^"%s^" -- Error #%i: %s",mkQuery,errnum,error);
	}
	
	// finished queries
	if(queryNum && ++queryIter >= queryNum)
	{
		queryIter = 0;
		queryNum = 0;

		set_task(1.0,"delete_update_flag");
	}
}

// get rid of the update flag in our database
public delete_update_flag()
{
	get_pcvar_string(cvTable,table,31);
	formatex(mkQuery,511,QUERY_DELETE_UPDATEFLAG,table,serverip);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0]) log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);

	SQL_FreeHandle(query);
}
And i think it's in this part it reads the file or something:
Code:
// transfer stats file to website
public update_database()
{
	new sfFile[64], sfLineData[81], sfAuthid[48], sfWins[6], sfName[64], sfTimestamp[12], sfPoints[8];

	get_cvar_string("gg_stats_file",sfFile,63);
	trim(sfFile);
Reply With Quote
  #7  
Old 05-19-2009, 03:26 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

That to me looks like the definition of some variables and then a call to a function (get_cvar_string) which places a string into sfFile then trimming that string.

Is there something specific you want to know about this?

PS. Yes, looks like C++ to me too.
__________________
Chris

Last edited by wildfire1982; 05-19-2009 at 03:26 AM. Reason: Added PS
Reply With Quote
  #8  
Old 05-19-2009, 07:19 AM
Qua's Avatar
QuaReading a Textfile and sorting things out? Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

The code is a snippet written in AMX Mod which is an extremely popular scripting engine for half life and it's descendants.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #9  
Old 05-19-2009, 08:01 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Is it possible to convert some of it to Visual Basic?
Reply With Quote
  #10  
Old 05-19-2009, 11:20 AM
Qua's Avatar
QuaReading a Textfile and sorting things out? Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Quote:
Originally Posted by Cyb3rH4Xter View Post
Is it possible to convert some of it to Visual Basic?
Of course it is possible. It will hardly prove to be viable or effecient to do so, however. What you're are asking is a very basic task involving file IO and parsing of specific files alongside with inserting data into a database.

If you're familiar with reading data from a file, then you'll need to do a simple parse of the data. The parsing could go something like:
  1. Read a line from the file
  2. Split the line into substrings delimited by a space
  3. Convert each subsstring into wanted data type
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #11  
Old 05-19-2009, 12:58 PM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Quote:
Originally Posted by Qua View Post
Of course it is possible. It will hardly prove to be viable or effecient to do so, however. What you're are asking is a very basic task involving file IO and parsing of specific files alongside with inserting data into a database.

If you're familiar with reading data from a file, then you'll need to do a simple parse of the data. The parsing could go something like:
  1. Read a line from the file
  2. Split the line into substrings delimited by a space
  3. Convert each subsstring into wanted data type
Great! Then we start with the first one.
Here's the code i think i'll need for that:
Code:
Dim fileReader As New System.IO.StreamReader("C:\ggstatsx.stats")
        Dim line As String = fileReader.ReadLine
        MsgBox(line)
Btw, the msgbox i only did for testing purposes. So, is this right?
Reply With Quote
  #12  
Old 05-20-2009, 03:03 AM
wildfire1982 wildfire1982 is offline
Senior Contributor
 
Join Date: Jul 2003
Location: Southampton UK
Posts: 801
Default

I suppose that depends whether it worked in the way you wanted it to Looks good to me so far.
__________________
Chris
Reply With Quote
  #13  
Old 05-20-2009, 08:46 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Well i want it to read a line and display it in the message box.....And it works
Then we go to step 2. Split the line into substrings delimited by a space.

Anyone wanna help me with that one?
I have come this long at the code:

Code:
Dim fileReader As New System.IO.StreamReader("C:\ggstatsx.txt")
        Dim line As String = fileReader.ReadLine
        MsgBox(line)

        Dim split As String() = line.Split(New [Char]() {" "c, " "c, " "c, " "c})
But know i have a problem, the line it gonna separate looks like this:
Code:
ip:port wins username timestamp points
If a user have a space in his name it would be separated.
Instead of it comes up like this:
iport
wins
username
timestamp
points

It would look like this:
iport
wins
user
name
timestamp
points

And that would totally be wrong.
How should i prevent this from happening?

Last edited by Cyb3rH4Xter; 05-20-2009 at 11:02 AM.
Reply With Quote
  #14  
Old 05-21-2009, 06:08 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

*BUMP* Anyone?
Reply With Quote
  #15  
Old 05-21-2009, 06:46 AM
Qua's Avatar
QuaReading a Textfile and sorting things out? Qua is offline
Impetuous & volatile

* Expert *
 
Join Date: Apr 2005
Posts: 2,177
Default

Don't allow your user names to contain spaces if that's the character you want to use for delimiting the content of the file. Another solution (altough kind of slacky) would be to count the number of substrings after the split. If there were more than 5 substrings you would know that the user name contained spaces, and you could concatenate the strings.
__________________
Reading is the foundation for all knowledge - Unknown.
Reply With Quote
  #16  
Old 05-21-2009, 10:13 AM
AtmaWeapon's Avatar
AtmaWeaponReading a Textfile and sorting things out? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

Other solutions you missed:
  • Use a delimiter other than space (this still limits the user name to a specific set of characters.)
  • Save user names as quoted strings, for example "With Space". This prohibits quotes as part of user names.
  • Escape the spaces in user names; for example, consider "+" to mean a space (URL encoding does this.)
  • Use a file format that doesn't treat whitespace as significant; XML wouldn't have this problem.
Most of these aren't possible because it sounds like you don't contain the file format. From the looks of it, a valid line where a user name contains a space might look like this:
Code:
127.0.0.1:1337 10 User Name 15
Regular expressions can help you here. You could separate things into fields, consider the first two and last one to be "non-user name", then consider the middle ones the user name, but this seems kind of messy.

Regular expressions themselves can seem pretty daunting, but they can do a lot of things that are really hard with Split() though there are still plenty of things they can't do. Here's a class that extracts the information from a line using a (arguably) simple regular expression:
Code:
Public Class GameInfo
    Private Const NumberOfFields As Integer = 5

    ' Use properties in real code; I'm using fields for brevity.
    Public IpAddress As String
    Public Port As Integer
    Public Wins As Integer
    Public UserName As String
    Public Points As Integer

    Public Shared Function Parse(ByVal logLine As String) As GameInfo
        Dim info As New GameInfo

        Dim pattern As String = "(?<IP>\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}):(?<port>\d+) (?<wins>\d+) (?<userName>.*) (?<points>\d+)"

        Dim lineMatch As Match = Regex.Match(logLine, pattern)

        If Not lineMatch.Success Then
            ' Invalid line, possibly return an error
            Return info
        End If

        ' Group 0 is always the entire capture
        If lineMatch.Groups.Count <> NumberOfFields + 1 Then
            ' Invalid line, possibly return an error
            Return info
        End If

        info.IpAddress = lineMatch.Groups("IP").Value
        info.Port = CInt(lineMatch.Groups("port").Value)
        info.Wins = CInt(lineMatch.Groups("wins").Value)
        info.UserName = lineMatch.Groups("userName").Value
        info.Points = CInt(lineMatch.Groups("points").Value)

        Return info
    End Function

    Public Overrides Function ToString() As String
        Return String.Format("{0} ({1}:{2}) had {3} wins and {4} points.", _
                             UserName, IpAddress, Port, Wins, Points)
    End Function

End Class
Separating the logic into a class has the advantage of keeping the data and logic in one place.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #17  
Old 05-22-2009, 02:49 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

thx for your answers. The problem is that it is not me that makes this file, is another program, then i can't manage the style of the file. There is already a program that does this but it's tooo buggy and won't work sometimes. That's why i wanted to translate the amxx code above to visual basic. Cause the code can read the lines and separate them. It is when it comes to the inserting to mysql it ****s up. And there is no-one that wants to fix it, cause no-one is maintaining it anymore.

So, then we're here again, is it possible to convert the code, or can someone here tell me what it does, which method it uses when reading the lines?

And again, thx for the great answers!
Reply With Quote
  #18  
Old 05-22-2009, 07:15 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Okey, i have looked into the code and found out which part that makes the "reading".
Here:
Code:
// transfer stats file to website
public update_database()
{
	new sfFile[64], sfLineData[81], sfAuthid[48], sfWins[6], sfName[64], sfTimestamp[12], sfPoints[8];

	get_cvar_string("gg_stats_file",sfFile,63);
	trim(sfFile);

	// stats disabled/file doesn't exist
	if(!sfFile[0] || !file_exists(sfFile))
	{
		log_amx("Could not update database: stats file ^"%s^" does not exist",sfFile);
		return;
	}

	// clear old entries
	get_pcvar_string(cvTable,table,31);
	format(mkQuery,511,QUERY_CLEAR_TABLE,table,serverip);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0])
	{
		log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);
		SQL_FreeHandle(query);
		return;
	}
	SQL_FreeHandle(query);
 	 
	// insert update flag
	formatex(mkQuery,511,QUERY_INSERT_UPDATEFLAG,table,get_systime(),serverip);

	query = SQL_PrepareQuery(db,mkQuery);
	SQL_Execute(query);

	SQL_QueryError(query,error,1023);
	if(error[0])
	{
		log_amx("Could not execute query ^"%s^" -- Error: %s",mkQuery,error);
		SQL_FreeHandle(query);
		return;
	}
	SQL_FreeHandle(query);

	new lines, file = fopen(sfFile,"rt");
	if(!file) return;
	
	queryIter = 0;
	queryNum = 0;

	while(!feof(file))
	{
		// one of our threaded queries produced an error
		if(insert_error)
		{
			fclose(file);
			return;
		}

		lines++;
		fgets(file,sfLineData,80);

		// isolate authid
		strtok(sfLineData,sfAuthid,23,sfLineData,80,'^t');

		// isolate wins
		strtok(sfLineData,sfWins,5,sfLineData,80,'^t');

		// isolate name
		strtok(sfLineData,sfName,31,sfLineData,80,'^t');

		// isolate timestamp (and points)
		strtok(sfLineData,sfTimestamp,11,sfPoints,7,'^t');

		// clean to prevent SQL injection
		replace_all(sfAuthid,47,"'","\'");
		replace_all(sfName,63,"'","\'");

		// put it in the database
		queryNum++;
		formatex(mkQuery,511,QUERY_INSERT_DATA,table,sfAuthid,str_to_num(sfWins),sfName,str_to_num(sfTimestamp),str_to_num(sfPoints),serverip);
		SQL_ThreadQuery(tuple,"sql_query_handler",mkQuery);
	}

	fclose(file);

	if(!insert_error) log_amx("Updated the database with %i entries",lines);
}
I uploaded the whole scriptfile if you want to read the rest.
It's around the "isolate" part the script.

I also looked at your code, AtmaWeapon, and i seem to understand what it does. btw, i missed to include that it was a timestamp in the field to, so a line would look like this:

Code:
127.0.0.1:1337 wins username timestamp points

127.0.0.1:1337 5 sampleuser 1242848852 350
The second line is an example line. The timestamp is always 10 numbers long.
When i paste your code into vb, i get an error in this line:
Code:
Dim lineMatch As Match = Regex.Match(logLine, pattern)
It says: Type Match is not defined.
Attached Files
File Type: txt gungame_sql.txt (7.5 KB, 1 views)

Last edited by Cyb3rH4Xter; 05-22-2009 at 07:36 AM.
Reply With Quote
  #19  
Old 05-22-2009, 09:04 AM
AtmaWeapon's Avatar
AtmaWeaponReading a Textfile and sorting things out? AtmaWeapon is offline
Fabulous Florist

Forum Leader
* Guru *
 
Join Date: Feb 2004
Location: Austin, TX
Posts: 9,500
Default

That error means that, with the current set of Imports statements, the VB compiler cannot find the class you specified. Match is actually System.Text.RegularExpressions.Match (as is Regex), so if you haven't imported the System.Text.RegularExpressions namespace then you will get that error.

Here's two general processes for solving this problem in the future; the first method might require VS 2008 or later.
  1. Place the cursor on the name of a Type that is causing the error (in this case, put the cursor on "Regex" or "Match".
  2. Push CTRL + . (that is, CTRL and the period character)
  3. In the popup menu, click "Import System.Text.RegularExpressions". This causes Visual Studio to import the namespace for you.

-OR-[list=1][*]Figure out which class is causing the problem (the error message points this out.)[*]Search MSDN for that type. In this case you could Google for Match to get to the Match class documentation.[*]Find the "Namespace" field; this is the namespace in which the class resides.[*]At the top of your file, add a line that looks like "Imports Namespace", where "Namespace" is the full namespace from the last step.[/url]

Sometimes neither method fixes things because the class is in an assembly that is not referenced; in that case, follow the 2nd method to get to the class documentation and look for the "Assembly" field. Then follow this process:
  1. Right-click your project and choose "Add Reference".
  2. In the dialog that appears, look for the assembly indicated in the documentation; select it and click "OK".

You'll have to alter the regular expression to respect this timestamp. If it's always a 10-digit number, you could add this to the appropriate part of the regex:
Code:
(?<timestamp>\d{10})
This says, "match 10 digit characters and store them in a group named 'timestamp'". Don't forget to put appropriate numbers of spaces around it.
__________________
.NET Resources
My FAQ threads | Tutor's Corner | Code Library
I would bet money 2/3 of .NET questions are already answered in one of these three places.
Reply With Quote
  #20  
Old 05-22-2009, 11:54 AM
Cyb3rH4Xter's Avatar
Cyb3rH4Xter Cyb3rH4Xter is offline
Freshman
 
Join Date: May 2009
Posts: 37
Default

Great! Thx for reply! Now it works! How do i make so the code read all the lines in the txt and adds an item to a listview detailed view for each line, something like this:
Code:
ListView1.Items.Add(Username,Wins,Points,Port,IP)
If you help me with that then i think i will be able to continue to the next step in my program.
Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump

Advertisement:





Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
subscribe
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
subscribe
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..
subscribe
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out? Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out? Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
 
Reading a Textfile and sorting things out?
Reading a Textfile and sorting things out?
 
-->