Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how to filter dates from a text file ?

Status
Not open for further replies.

zxcxz

Programmer
Jan 4, 2007
4
TR
hi to all. i'm new in C. then, i've to code a programme that filters dates from a text file. for example ;

text file includes :

skdfjdfkj01/01/2007kjeu3jfk
01.01/2007jskajf4-2-2006dsks

i've to filter 01/01/2007 - 01.01/2007 and 4-2-2006 from the file. and also write into the output. how can i do that ? :-(
 
This might be faster and easier for you to write in a scripting language like VBScript or Perl.

Do you know any scripting languages very well?
 
I know C and Java. But not well. Also I must write it in C or Java :(
 
This is a trivial problem for sed. Is this a school assignment?
 
Yes, this is an assignment. Also, I don't know how to filter dates. I think I can open textfile, etc. But I can't filter dates from the file. So, that's why I can't code it :(
 
Code:
[red]skdfjdfkj[/red]01/01/2007kjeu3jfk
01.01/2007jskajf4-2-2006dsks
Well is all the 'junk' always character data, or can it contain numbers as well?

The first step would be to show that you can open the file and just print the contents of the file 'as is', without applying any kind of transformation on the data. Until that is done, it's not worth thinking about the rest of the problem.

When file reading is fine, then look at the functions in ctype.h (specifically say isdigit() ).

--
If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
 
Also, can the dates be anywhere within the junk, or is the file formatted somehow so that the dates are always at the same position on each line?

Does it have to be in C or Java, or can it also be in C++?
 
Try looking at the man pages for [tt]regcomp()[/tt] and [tt]regexec()[/tt]. These functions are for handling regular expression matching. A regular expression to match the date examples you gave would be "[tt][0-3]?[0-9][./-][0-1]?[0-9][./-][12]?[0-9]?[0-9][0-9][/tt]" (assuming mm/dd/yyyy).

Here's a link to the Wikipedia entry on regular expression matching...

 
SamBones said:
Try looking at the man pages for regcomp() and regexec()
If you don't care about portability, this might make things easier. Otherwise you might want to check something like to see if they have any good regex libraries that are a little more portable. (or you could write it in Java)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top