Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

  Tutorial: C and Cpp File Manipulation Searching for a string in a File Tutorial

How to find the first occurance of a string within a file, includes sample function.

Tutorial Details:

I started working on a project that was to replace the often times buggy and slow (and in my opinion just plain bad) Find Files/Folders function that comes with Windows (windows key + 'F'). In Windows XP the searching utility in the OS seems to be severly lacking in functionality. In previous versions of Windows I used i didn't find there to be too many problems.

The most important part of my project was searching for text within files, something which the Find Files/Folders function claims it can do but it never seems to return results even when I know there should be some. This is what caused me to look for a nice way to search for text inside of a file in much the same way strstr searches for a string inside a larger string. I did find a solution somewhere out there on the web but for reasons I still can't figure out (the code was very messy) it would stop actually looking once you searched through about 16MB worth of files in one run.

Since I could not find anything out there that would allow me to do very extreme amounts of file searching I had to make it myself. What i created is designed to generally be platform independent. Normally I do not write code to be this way because 99.9% of the time I develop things exclusively for Windows.

Because I happen to like them better this will be done with plain old C-style file functions for several reasons:
1. They're MUCH faster than C++ filestreams.
2. The compiled code is MUCH smaller than code using C++ filestreams.
3. They're compatable with old C code.
4. The code looks nicer (to me at least).
5. The project I was working with was using them.
6. They're fun and you should learn to use them.

I'm also going to be using malloc() and free() instead of new and delete. No real reason other than to make this code more C complient even though it is meant to be C++ code. And of course I'll be using C-stlye strings and C-syle string functions, I always do this with code I plan on recycling, because if I ever put it in a DLL I can rest assured programs written in another language will be able to use the function. A program written in VB won't be able to make use of a function inside a DLL that returns a std::string, but it can make use of a function that returns a pointer to a C-style string.


 

Read Tutorial at: Click here to view the tutorial

Rate Tutorial:
C and Cpp File Manipulation Searching for a string in a File Tutorial

View Tutorial:
C and Cpp File Manipulation Searching for a string in a File Tutorial

Related Tutorials:

Java Q&A - Java Still Open
Java Q&A - Java Still Open
 
Automatic Software Distribution of Java Applications
Automatic Software Distribution of Java Applications
 
Java Tip 85: Fun and games with JFileChooser - JavaWorld
Java Tip 85: Fun and games with JFileChooser - JavaWorld
 
Using XML and JSP together - JavaWorld March 2000
Using XML and JSP together - JavaWorld March 2000
 
The Lucene search engine: Powerful, flexible, and free - JavaWorld September 2000
The Lucene search engine: Powerful, flexible, and free - JavaWorld September 2000
 
The Gnutella file-sharing network and Java - JavaWorld October 2000
The Gnutella file-sharing network and Java - JavaWorld October 2000
 
Leverage legacy systems with a blend of XML, XSL, and Java - JavaWorld October 2000
Leverage legacy systems with a blend of XML, XSL, and Java - JavaWorld October 2000
 
Talking Java! - JavaWorld August 2001
Talking Java! - JavaWorld August 2001
 
Create your own type 3 JDBC driver, Part 3
Create your own type 3 JDBC driver, Part 3
 
Java's character and assorted string classes support text-processing
Java's character and assorted string classes support text-processing
 
Navigate through virtual worlds using Java 3D
Navigate through virtual worlds using Java 3D
 
JSP 2.0: The New Deal, Part 4
JSP 2.0: The New Deal, Part 4 In this final part of the "JSP 2.0: The New Deal" series, we look at two new features that make it much easier to develop custom tag libraries: tag files and the new simplified tag-handler Java API.
 
Java and .Net interoperability using CORBA
Java and .Net interoperability using CORBA A recently released product takes the pain out of implementing object-level calls between Java and .Net While the need for interoperability between Java and .Net has become a common problem in larger organ
 
Develop Your Own Plugins for Eclipse, Part 1
This article series is intended provide you the basic information necessary to quickly code your first plugin. The resources section will point to all of the necessary introductory materials.
 
XML DOM-lite parser and writer
Summary This article provides a simple method for creating valid XML and a simple DOM-like (Document Object Model) parser. This method is useful for small applications that need simple XML functionality without the size and complexity of the full range d
 
The JDBC RowSet Implementations Tutorial
In "The JDBC RowSet Implementations Tutorial," you will look at how to use the standard JDBC RowSet implementations specified in JSR-114.
 
JSP Tutorial
Adding dynamic content via expressionsAs we saw in the previous section, any HTML file can be turned into a JSP file by changing its extension to .jsp. Of course, what makes JSP useful is the ability to embed Java. Put the following text in a file wit
 
Backing Up and Restoring A MySQL Database
Backing Up and Restoring A MySQL Database Backing Up and Restoring A MySQL Database This tutorial explains the how to backup and restore the MySQL Database. Databases are used to store large amount of precious data and it becomes very important to
 
10 Minutes Guide to Ant
10 Minutes Guide to Ant 10 Minutes Guide to Ant Previous Tutorial Index Next Introduction Well for the next 10 minutes get ready to devote to the ant guide. This will make some sence to the ant. Ant is a free tool under GNU Licence and is
 
Building Web Application With Ant and Deploying on Jboss 3.0
Building Web Application With Ant and Deploying on Jboss 3.0 Building Web Application With Ant and Deploying on Jboss 3.0 Previous Tutorial Index Next In this lesson I will show you how to build you web application and install on the Jboss 3.0
 
Site navigation
 

 

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2006. All rights reserved.