Programming Tutorials Browser Tutorials Articles Struts Tutorials Hibernate Tutorials

Search: 

  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:

Displaying 1 - 50 of about 4434 Related Tutorials.

PHP File Manipulation Writing to a File Tutorial
File Manipulation in PHP File Manipulation in PHP...;  In this tutorial we will learn how to write data to a flat file... inside C:\wamp\www\php directory, where we will write the .php file): <?php
 
Java Write To File - Java Tutorial
Java write to File Example,How to Write to File Using FileOutputStream in Java Java Write To File - Java Tutorial... File created successfully. C:\nisha
 
C file open example
C file open example C file open example...;  This section demonstrates you how to open a file in C. ... that the fprintf() function write the string to the file MYFILE.txt.  Here i am using
 
Java read file line by line - Java Tutorial
; }--> Java read file line by line - Java Tutorial   ... reading and writing operation in a file. In the section of Java Tutorial you... C:\nisha>java ReadFile This is a text file
 
Data Manipulation Statements
Data Manipulation Statements Data Manipulation...;    Data Manipulation Statement is used to retrieve, insert... are describing the following Data Manipulation Statements : Select Statement Insert
 
Append To File - Java Tutorial
Java Append File,Append to File Example,Appending to a File in Java,Append to File in Java Append To File - Java Tutorial.... This constructor simply overwrite the contents in the file by the specified string
 
C file write example
C file write example C file write example...;  This section demonstrates you to write the data into the file. Here we are using the library function fwrite() to write the data into the file
 
C file read example
C file read example C file read example...;  This section demonstrates you to read a line from the file. You can see in the given example, we prompt the user to enter the name of the file
 
Searching an Element in the given XML Document
SearchElement.java C:\vinod\xml>java SearchElement Enter file name... Searching an Element in the given XML Document   ... asks for a  XML file name and checks it existence. If the given file
 
Constructing a File Name path
programming tutorial will teach you how you can construct a file name path. By using... Java FileName Path,java SeparatorChar,Constructing a File Name path Constructing a File Name path   
 
C file delete example
C file delete example C file delete example...;   This section illustrates you to delete a file in C. You can... on the console. Here i am using TurboC++ IDE and my source file is in the C
 
JSP Open File
into the file. Understand with Example  The Tutorial illustrate an example from JSP... system. For this, we have create a file 'Hello.txt' in the C:drive. The ... JSP Open File JSP Open File
 
C file rename
C file rename C file rename...; This section illustrates you to rename a file in C. You can see in the given example that we want to replace the name of the text file MYFILE.txt
 
File Reader example in JRuby
File Reader example in JRuby File Reader example...;     In this section of JRuby tutorial we will study to Read File in JRuby. We can read and write file on the console 
 
Java example program to get extension
;       java get extension To get the file name and file extension separately we can do the string manipulation on the file name. Suppose "filename.ext" is some filename with extension
 
C/C++ Programming Books
++ tutorial. For the beginner who doesn't know much about C or C++, it just... C/C++ Programming Books C/C++ Programming...;    Visual C++ 6 Unleashed
 
C String Copy
C String Copy C String Copy...; In this section, you will learn how to copy the string in C. You can see... by the header file <string.h> that will copy the specified string from st1
 
C String to Int
C String to Int C String to Int...; In this section, you will learn how to convert a string represented value into an integer represented value in C. You can see in the given example that we
 
Count lines of a particular file
Count Number of Lines,Count Lines of a Particular File,How to Count Lines in a File using Java IO Count lines of a particular file           
 
Java Change File-Extension
Java Display File-Extension Java Change File...;   In this program you will learn how to change the file extension of the file. Here you will be asked to enter the file name whose extension
 
Insert Data From File
file 'abc,txt'. LOAD DATA INFILE 'c:/abc.txt' INTO TABLE... Insert Data From File Insert Data From File...; Insert Data From File is  used to insert data from file
 
Java Get File Name
;      File file = new File("C... Java Get File Name Java Get File Name...; In this section, you will study how to obtain the name of file
 
Backup Into txt File
; Backup Into txt File is used to put the backup file from a  table into a text File. Understand with Example The Tutorial... Backup Into txt File Backup Into txt File
 
Create File in Java
C:\nisha>java CreateFile1 The specified file is already exist... Create File in Java,How to Create File in Java,Create File Using Java Code... Whenever the data is need to be stored, a file is used to store the data. File
 
Write Text into File
into file. We are creating file in which we are writing string by use of write... Write Text File Java,Java Write to File,Java Write to File Write Text into File       
 
Traversing a file and directory under a directory
Traversing a file and directory under a directory... as TraversingFileAndDirectories then make an object of File class and pass any directory and filename to its constructor. After that, check it whether the file
 
J2ME Read File
J2ME Read File J2ME Read File  ...; In this J2ME application, we are going to read the specified file. This example shows you how to read the data of the specified file. To implement this type of logic
 
Determining if a File or Directory exist
is given below: C:\java>java FileOrDirectoryExists the file... Check File Exists Java,Check Directory Exists,Code of File or Directory Exists in Java Determining if a File or Directory Exist
 
Retrieve Data from CSV file in JSP
; String fName = "c:\\csv\\myfile.csv";      String... Retrieve Data from CSV file in JSP Retrieve Data from CSV file in JSP        
 
Get File Size
a character stream from a file. We declare a string variable... in a string array file list. We display the list of array file using for loop. Now... Get File Size Get File Size
 
Change a file timestamp
C:\vinod\Math_package>java ChangeFileDate Change file timestamp... Change File Timestamp,Java Timestamp Example,How to Change File Timestamp Change a file timestamp   
 
Export Data into CSV file uing JDBC in JSP
;%  String filename = "c:\\csv\\myjdbcfile.csv"; Connection conn... Export Data into CSV file uing JDBC in JSP Export Data into CSV file uing JDBC in JSP     
 
Struts File Upload and Save
Struts File Upload and Save Struts File Upload... regarding  "Struts file upload example". It does not contain any code illustrating how to  save the file on the server . Now, the current example
 
Write to a file
Write to a file Write to a file... file. The given example uses the BufferedOutputstream class that writes the bytes to the output stream. The output stream is a file "Filterfile.txt"
 
Getting the Size of a File in Java
: C:\nisha>java FileSize Enter file name : myfile.txt Size of the file : 19 C:\nisha>... Getting File Size,Java File Size,Source Code to Find File Size in Java
 
Uncompressing the file in the GZIP format.
method inside the class which will take one String argument i.e. file name... Java GZIP Example, GZIP file format in Java Uncompressing the file in the GZIP format.     
 
C String uppercase
C String uppercase C String uppercase...;  In this section, you will learn how to convert a string value in to uppercase. In the given example, we have define a string in lowercase characters
 
Create Temp File
CTFile Created temporary file with name C:\DOCUME~1\ADMINI~1\LOCALS~1\Temp... Temp File Java,Create Temp File,How to Create Temporary File in Java Create Temp File   
 
Creating a temporary file
with() that will close the string. deleteOnExit(): It will delete the existing file when you... CreateTemporaryFile Temporary file file has been created : C:\DOCUME~1\ADMINI~1... Java Temporary File, Create Temporary Files, File Creation in Java
 
Delete temp file
Delete Temp File,How to Delete Temporary Files,Java Program to Delete Temp File Delete temp file   ...; In this section, you will learn how a temporary  file is deleted from the current
 
Java Write To File - Java Tutorial
Java Write To File,Java Write to File Example,Write to File in Java,How to Write to File Java Write To File - Java Tutorial...;  In the section of Java Tutorial you will learn how to write
 
Storing properties in XML file
Storing properties in XML file,XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials Storing properties in XML file  ...; This Example shows you how Store properties in a new XML File. JAXP (Java API
 
C String lowercase
C String lowercase C String lowercase...;  In this section, you will study how to convert the string to lowercase. You can see in the given example, we have define a string in uppercase
 
Reading File into ByteArray
C:\chandan>java FileReadInByteArray Your File Content... Reading File into ByteArray Reading File...;   In this section, you will learn how to make a file
 
Backup File With Separator
Backup File With Separator Backup File With Separator...;   Backup File With Separator in SQL  is used to indicate line and record separator. Understand with Example The Tutorial illustrate
 
Forcing Updates to a File to the Disk
Java Save File,Forcing Updates to a File to the Disk Forcing Updates to a File to the Disk     ... are going to update a file forcefully. Firstly we have to make an object
 
Delete file or Directory
Delete file or Directory Delete file or Directory...;  In this example we are discussing the deletion of a file or a directory by using a java program. We pass the file name or the directory name
 
pdf file measurement
pdf file measurement pdf file measurement... in the document. In this example we need iText.jar file, without the jar file... and java.io.IOException classes are used to create file and throw the exception, if there is any
 
Copy Directory or File in Java
will see how contains of a directory or a file is copied  from the specified... then the program gives the output message like : File or directory does not exist... are explained ahead :  copyDirectory (File srcPath, File dstPath
 
C String Concatenation
C String Concatenation C String Concatenation... strings in C. You can see in the given example, we have declared two strings...() provided by the header file <string.h>. This method takes two char
 
Site navigation
 

 

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

Copyright © 2006. All rights reserved.