Skip to main content
gamedev.net
🔒 Locked

strings c++

Started by sarkath Apr 11, 2002 at 9:18 PM 3 replies 200+ views
Original Post
sarkath
sarkath
small problem... making a logfile analyzer (or whatever) It scans the logfile line by line... and when it finds a line "user ulrich logged at 14.34.03 02/10/01." I want to extract the username(which of course can differ in length)... but I have found no way of doing this with standard string library or any other way. I am probably barking up the wrong tree even, but im tired and am going to sleep.... any minute now. any ideas ? and yes im a newbie.
eldee
eldee
userlocation = mystring.find("user", 1) + 5; // this will set the position after the space
endofname = mystring.find(" ", userlocation);
username = mystring.substr(userlocation,endofname);

simple enough, off the top of my head so i dunno if
it''ll compile
good luck


-eldee
;another space monkey;
[ Forced Evolution Studios ]

::evolve::

Mulligan
Mulligan
When you come across that line, start at str[5] (The first letter of the user name) and run a while loop that copies each individual letter until the letter being read is a ''\0'' (white space).

More or less, this is what you want. I don''t have a compiler so it probably does not work right

char* line = "user ulrich logged at 14.34.03 02/10/01.";
char username[256];
char ch = line[5];
int ctr = 0;

while( ch != ''\0'' )
{
username[ctr] = ch;
ctr++;
ch = line[ctr+5]; // this may be off by one...4?
}
username[ctr] = ''\0'';
sarkath
sarkath
Great ideas thanx mates. I''ll check them as soon as i regain counciousness tomorrow :-))
Chem0sh
Chem0sh
You could also do:


  
char* line = "user ulrich logged at 14.34.03 02/10/01.";
char username[256] = {''\0''}; /* I like to clear my strings to be safe */
char dummy[4] = {''\0''};

sscanf(line, "%s %s", dummy, username);



Also, if you do it Mulligan''s way, you need to check for '' '' (whitespace) and not ''\0'' (end of string).


/*=========*/
/* Chem0sh */
/*=========*/
/*=========================================// Chem0sh// Lead Software Engineer & Tech Support// http://www.eFaces.biz=========================================*/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.