Code Newbie
News     Forums     Search     Members     Sign Up    

My Code Newbie
Username

Password

Articles/Snippets
ASP Classic
ASP.NET
C
C#
C++
HTML / CSS
Java
Javascript
Linux / BSD
Perl
PHP
Python
Ruby
SQL
VB 6
VB.NET

C.N. Friends
  Planet Rome

Link to Us!
Code Newbie
  Code Newbie
    forums
Old 08-19-2004, 12:13 AM   #1 (permalink)
iwanttolearnc
Registered User
 
iwanttolearnc's Avatar
 
Join Date: Jun 2004
Posts: 11
iwanttolearnc is on a distinguished road
file scanning

I am doing some code in C to scan a file for the occurance of perticular string or pattern. I can implement this by reading the file in some fixed size to a buffer and then searching for the pattern.
The problen is
supposse that part of the pattern exists at the end of the buffer
and then the remaining with come up with te next read only,then
simple string comparison fails.
A simple fix would be to read the whole file in the buffer and then
compare, which is not feassible always.

Can somebody suggest a solution, any idea how file scanners and programs like Grep do it.

The code has to run for windows or linux so I cannot resort to any
MFC , C++ can be used
iwanttolearnc is offline   Reply With Quote
Old 08-19-2004, 03:06 AM   #2 (permalink)
redhead
Newbie
 
redhead's Avatar
 
Join Date: Jun 2002
Location: Denmark
Posts: 1,726
redhead is on a distinguished road
Just in theory, perhaps not the most convenient way of achieving it, but here it goes.
Code:
 
  1) Read the desired buffer_length of the file 
     (could be a single line/ portion of a line/ whatever)
  2) Search for the patern / or as much of the first part of the
     patern, when getting closer than strlen(pattern) to end of buffer
  3) If the first part of the pattern matched at the end of the buffer
     search for the remainder of the pattern as the beginning of the
     next read from file, skipping what ever newline char there might
     have been, if the pattern was split over two lines in the file.
__________________
Don't worry Ma'am, We're university students, We know what We're doing.
-----
If you pull the pin, Mr.Grenade would no longer be your friend.
-----
01000111 01101111 00100000 01000011 00100000 00100001
redhead is offline   Reply With Quote
Old 08-19-2004, 08:28 AM   #3 (permalink)
iwanttolearnc
Registered User
 
iwanttolearnc's Avatar
 
Join Date: Jun 2004
Posts: 11
iwanttolearnc is on a distinguished road
thanks redhead
is there any optimised algorithm that can speed up the search process
and is there someother way thst using strstr()to detect the pattern.
A lot of software provides support fot search and replacement so there must be some standard technique.

Speed is of some concern here,it mustn't hog all the CPU time
iwanttolearnc is offline   Reply With Quote
Reply

Bookmarks

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT -8. The time now is 04:23 AM.


Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.0.0 RC8





Copyright © 2000-2008, Milano Interactive
Web Hosting provided by Portal 360 Web Hosting