Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by scootz3345
however, the output takes the value of date and also the next string..
Well...that should be my fault I guess....it should have been
Code:
date = t.substr(pos + pattern.length() , t.find(" ", pos) - (pos + pattern.length()));
Quote:
Originally Posted by scootz3345
and please if can u explain a bit how the code works so that i can expand it for other variables.
I have added comments to my previous reply....
Re: how to open a file (example .txt) using VC++??
Dear scootz3345,
For this kind of work the Perl language is a much better choice, especially if you have to cope with more difficult text parsing tasks. Since you seem rather unexperienced in the C++ language maybe this is the time to switch. Your example would be expressed in Perl as follows:
Code:
# Open file
open(IN, "samplesyslog.txt") or die "Can't open your file\n";
# Read line by line and find the last occurrence of "date="
while (<IN>) {
# Store text next to it up to the next whitespace in $date
$date = $1 if /date=(\S*)/ ;
}
# Close file
close(IN);
# Print result
print "$date\n";
Just store the code in the file syslogdate.pl, no need to compile and link.
Sample data in file samplesyslog.txt
Code:
blah blah
system date=01/02/2005
Now let's try a date followed by other stuff:
and another date=02/03/2005 time=18:34
note that the word date without an = immediately following it won't match.
Then type syslogdate.pl at the command prompt:
Code:
D:\Proj\Perl>syslogdate.pl
02/03/2005
Perl should have been installed first, it is available for free download. Start at http://www.perl.org
Re: how to open a file (example .txt) using VC++??
OK, now about parsing YOUR sample syslog. Try this:
Code:
open(IN, "samplesyslog.txt") or die "Can't open your file\n";
# read line by line and find the last occurrence of a number of named fields
while (<IN>) {
# Extract several fields and store them in variables
$date = $1 if /\bdate=(\S*)/ ;
$time = $1 if /\btime=(\S*)/;
$log_id = $1 if /\blog_id=(\S*)/;
$service = $1 if /\bservice=(\S*)/;
}
close(IN);
print "Log timestamp is $date, $time (Reference ID=$log_id)\n";
print "File Transfer Protocol\n" if $service eq "ftp";
print "Domain Name Service\n" if $service eq "dns";
The program output will be:
Code:
D:\Proj\Perl>syslogdate.pl
Log timestamp is 2005-04-26, 18:36:56 (Reference ID=0022010001)
File Transfer Protocol
Note the \b prefix in "\btime" was used to match only "time" starting at a word boundary.
Exercise for the reader: Try to get the same result in C++. And ask yourself which program is more elegant, self-explanatory.
Note: I am an experienced C++ programmer, but for text parsing jobs I can't beat Perl.
Re: how to open a file (example .txt) using VC++??
thanx guys..ya..its deffinately easier to do it in Perl with the foreach() function. but its already set for me to do it in C++, kinda difficult.
I will try to expand the codes. Thanx again!!
1 Attachment(s)
Re: how to open a file (example .txt) using VC++??
Adreas, thanx man...wanna ask u something, is it the "std::string pattern("date=");" can only be used once??
which part of the codes should i modify if i wanted to display other variables??
and one more thing, the codes only takes the last time value..if u go though the file there is actually a lot of time value..
can u help modify the codes so that the output will be something like this??
i'll send u a sample codes that produce the output..
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by Ejaz
When in Roam, do as Romans do ;) When using MFC, do as MFC does, when using C++, do as C++ does and when using C, do as C does. :thumb:
But when you are using MFC, you must be using C++. I can't see any reason to use MFC CFile over C++ streams. Remember MFC is very old now and was invented before Microsoft was especially interested in adding the STL to their compiler.
Markus
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by yash_sws
Sometimes, I wonder why people no longer use good ol' C.
I mean what is it in CFile that you cant do with FILE.
here's what i would do
#include <cstdio>
int main()
{
FILE *fin;
FILE *fout;
char buffer[512];
int read;
fin = fopen( "example.txt", "rb" );
if( !fin ) return -1;
fout= fopen( "newfile.txt", "wb" );
if( !fout ) { fclose(fin); return -1; }
while(!feof(fin))
{
read=fread( buffer, 1, 512, fin );
fwrite( buffer, 1, read, fout );
}
fclose(fin);
fclose(fout);
return 0;
}
I wonder why this compiles. According to footnote 160 in the section 17.4 of the C++ standard, if you include the newer headers (cstdio instead of stdio.h etc) then the names from those headers go into namespace std and not into the global namespace.
Markus
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by scootz3345
Adreas, thanx man...wanna ask u something, is it the "std::string pattern("date=");" can only be used once??
What do you mean with only once?
Quote:
Originally Posted by scootz3345
which part of the codes should i modify if i wanted to display other variables??
You need to understand how the concept works...you read the file line-by-line...thus, you read in one line and parse it. While having this line, you can search for whatever you want. So...in other words....the code within the while loop in my example exactly deals with one line...
Quote:
Originally Posted by scootz3345
and one more thing, the codes only takes the last time value..if u go though the file there is actually a lot of time value..
What time value? My code was looking for the date in every line... :confused:
Re: how to open a file (example .txt) using VC++??
oh ya!! your example was using date, it works fine because date only have
one value..
however when i change "date=" to "time=" it only takes the last value of
time that is in the last paragraph of the file...it doesnt work,
in that file, actually there's a lot of time value...which is different for every
log file.
example =
Apr 26 18:33:39 192.168.2.1 date=2005-04-26 time=18:36:38 device_id=APS3012404200864 log_id=0022010001 type=traffic subtype=allowed pri=notice vd=root SN=16863 duration=130 policyid=4 proto=6 service=ssh status=accept src=210.101.234.131 srcname=210.101.234.131 dst=202.189.48.98 dstname=202.189.48.98 src_int=external dst_int=dmz/ha sent=48 rcvd=76 sent_pkt=1 rcvd_pkt=1 src_port=9218 dst_port=22 vpn=n/a tran_ip=203.223.134.2 tran_port=22 dir_disp=org tran_disp=noop
"18:36:38"
Apr 26 18:33:42 192.168.2.1 date=2005-04-26 time=18:36:40 device_id=APS3012404200864 log_id=0022010001 type=traffic subtype=allowed pri=notice vd=root SN=16991 duration=36 policyid=1 proto=6 service=ftp status=accept src=192.168.2.106 srcname=192.168.2.106 dst=209.133.111.196 dstname=209.133.111.196 src_int=internal dst_int=external sent=1003 rcvd=1889 sent_pkt=14 rcvd_pkt=18 src_port=2333 dst_port=21 vpn=n/a tran_ip=202.189.48.98 tran_port=54490 dir_disp=org tran_disp=noop
"18:36:40"
Run the previous codes that i attached above, u'll know what i'am tryin to explain ...sorry for the misconclusion
Re: how to open a file (example .txt) using VC++??
Well...I don't know why you only have one date....every entry in the given file has one date and one time value.
Thus...the code given should work for both date and time...simply exchange 'date=' with 'time=' and it will give you the time instead...
1 Attachment(s)
Re: how to open a file (example .txt) using VC++??
yeahh..i did change the date with time, but the problem is the file have
different time value in different paragraph (every syslog has the same date,
but different time value) i try to change the date in your codes with time,
what it print only the last value of time. (time value from the last paragraph
in the file)
Try o run this attachment file: Can u help me modify your codes into something like this??
thanx !
Re: how to open a file (example .txt) using VC++??
Well...is there any reason why you are using old-style character strings instead of the STL 'string' for searching? This is most likely the reason why it behaves different from what I have been posted... ;)
I would need to look deeper into it...however, currently I am a little bit busy for that...you would need until tonight...
Re: how to open a file (example .txt) using VC++??
No problem..just take your time..yeah..i prefer your way too..
thats y probably you can help me..thanx man!!
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by scootz3345
yeahh..i did change the date with time, but the problem is the file have
different time value in different paragraph (every syslog has the same date,
but different time value) i try to change the date in your codes with time,
what it print only the last value of time. (time value from the last paragraph
in the file)
That's because the output statement in Andreas' sample in fact belongs inside the while loop in order to print the date (or time, for that matter) for every log entry.
Quote:
Originally Posted by scootz3345
Try o run this attachment file: Can u help me modify your codes into something like this??
Re your attachment openfile1.cpp: Questionable coding.... For several reasons:
1) You assume a fixed length of every value in the log file. For date/time values this may be ok, but not for most of the other values. You'd better gather characters until the first blank (assuming no blanks can appear inside an attribute value).
2) Why the flag argument and the huge switch statement instead of directly supplying the value length as an argument?
3) You can't make a temporary copy by just You only copy the pointer in that case, but both pointers will refer to the original string.
4) Trying to get the values of the "sent" and the "sent_pkt" fields, for example, in this manner won't work (unless they happen to occur in a particular order)
You seem to be missing the basics of the C++ language. I would recommend you to start with a C++ tutorial or use a more beginner-friendly language.
Re: how to open a file (example .txt) using VC++??
Quote:
Originally Posted by Adreas Maseur
is there any reason why you are using old-style
character strings instead of the STL 'string' for searching?
well..actually i didn't know about the STL 'string'..thats why i really need your help on this..thanx